Java时区解析DateFormat时 [英] Java Time Zone When Parsing DateFormat

查看:184
本文介绍了Java时区解析DateFormat时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码解析日期如下:

I had code that parses date as follows:

String ALT_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
SimpleDateFormat sdf = new SimpleDateFormat(
                    ALT_DATE_TIME_FORMAT);
Date date = sdf.parse(requiredTimeStamp);

它工作正常,突然,这停止工作。事实证明管理员在服务器上进行了一些配置更改,并且当前的日期被返回为2010-12-27T10:50:44.000-08:00,这不符合上述模式。我有两个问题:

And it was working fine, suddenly, this stopped working. It turns out an admin made some config changes on the server and the date is currently being returned as "2010-12-27T10:50:44.000-08:00" which is not parse-able by the above pattern. I have two questions:

第一个将是以什么样的格式解析JVM返回的日期(具体来说只是'-08:00')时区)?其次,linux RHEL 5服务器上的这些设置究竟在哪里改变,以便我们知道未来会有这样的变化?

The first would be what pattern would parse the date being returned by the JVM in the format above (specifically, just '-08:00' as the time zone)? And second, where exactly would one change such settings on a linux RHEL 5 server so that we are aware of such changes in the future?

推荐答案

其他应用程序正在使用ISO 8601 dateTime格式。我假设其他应用程序正在向您发送符合XML Schema的dateTime类型(即ISO 8601)的XML响应。现在,DateFormat无法解析此格式。您必须使用其他库,如joda-time(joda-time是获胜者)或FastDateFormat,在其他响应中指定。看看这篇文章将符合ISO8601的字符串转换为java.util.Date

The other application is using the ISO 8601 dateTime format. I am assuming the other application is sending you an XML response that is in compliance with XML Schema's dateTime type, which is ISO 8601. Now, it is a known thing that the DateFormat can't parse this format. You either have to use other libraries like joda-time (joda-time is the winner) or the FastDateFormat as specified in the other responses. Look at this post Converting ISO8601-compliant String to java.util.Date

这篇关于Java时区解析DateFormat时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆