解析 DateFormat 时的 Java 时区

2022-01-16 00:00:00 datetime timezone iso8601 date-format java

我有如下解析日期的代码:

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.看这篇文章Converting ISO 8601-compliant String to 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 ISO 8601-compliant String to java.util.Date

相关文章