简体   繁体   English

与Java 8 ZonedDateTime API不兼容的旧日期格式

[英]Old Date Formatting incompatible with Java 8 ZonedDateTime API

I am updating my old date formatting code to Java 8 and trying the ZonedDateTime API . 我将旧的日期格式代码更新为Java 8,并尝试使用ZonedDateTime API

The format of date is same as the Javascript Date object format, eg - 日期的格式与Javascript日期对象格式相同,例如-

Thu May 25 2017 10:00:00 GMT+1200 (New Zealand Standard Time) 2017年5月25日星期四,格林尼治标准时间+1200(新西兰标准时间)

I was using the below format previously - 我以前使用以下格式-

EEE MMM dd yyyy hh:mm:ss 'GMT'Z '('zzzz')' EEE MMM dd yyyy hh:mm:ss'GMT'Z'('zzzz')'

This format fails to parse the date string using DateTimeFormatter.ofPattern method. 此格式无法使用DateTimeFormatter.ofPattern方法解析日期字符串。

Here's the code: 这是代码:

public static final String DATE_FORMAT = "EEE MMM dd yyyy hh:mm:ss 'GMT'Z '('zzzz')'";

public static void main(String[] args) throws ParseException {
    String sDate = "Thu May 25 2017 10:00:00 GMT+1200 (New Zealand Standard Time)";
    parseDate(sDate);
}

private static void parseDate(String sDate) throws ParseException {

    // works
    DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
    Date oldDate = dateFormat.parse(sDate);

    //FIXME: can't parse?!
    ZonedDateTime newDate = ZonedDateTime.parse(
           sDate, DateTimeFormatter.ofPattern(DATE_FORMAT)); // <- this is the line 25!
}

Here's my full code for reference that can be compiled and run - https://gist.github.com/bhabanism/470e03db54981ad6ddedbba316dcaa9a 这是我完整的参考代码,可以编译和运行-https://gist.github.com/bhabanism/470e03db54981ad6ddedbba316dcaa9a

This fails at line#25 with: 这在第25行失败,并显示:

Exception in thread "main" java.time.format.DateTimeParseException: Text 'Thu May 25 2017 10:00:00 GMT+1200 (New Zealand Standard Time)' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {HourOfAmPm=10, MilliOfSecond=0, MinuteOfHour=0, OffsetSeconds=43200, MicroOfSecond=0, NanoOfSecond=0, SecondOfMinute=0},ISO,Pacific/Auckland resolved to 2017-05-25 of type java.time.format.Parsed 线程“主”中的异常java.time.format.DateTimeParseException:无法解析文本“ 2017年5月25日星期四10:00:00 GMT + 1200(新西兰标准时间)”:无法从TemporalAccessor获取ZonedDateTime:{HourOfAmPm = 10,MilliOfSecond = 0,MinuteOfHour = 0,OffsetSeconds = 43200,MicroOfSecond = 0,NanoOfSecond = 0,SecondOfMinute = 0},ISO,Pacific / Auckland已解析为java.time.format.Parsed类型的2017-05-25

Note, I can't change the input format of the Date, it has to be 请注意,我无法更改日期的输入格式,它必须是

Thu May 25 2017 10:00:00 GMT+1200 (New Zealand Standard Time) 2017年5月25日星期四,格林尼治标准时间+1200(新西兰标准时间)

I can surely modify the formatter 我肯定可以修改格式化程序

EEE MMM dd yyyy hh:mm:ss 'GMT'Z '('zzzz')' EEE MMM dd yyyy hh:mm:ss'GMT'Z'('zzzz')'

It seems there was a bug in your format string all the time. 似乎您的格式字符串中一直有错误。 Lowercase hh is for hour within AM or PM, in the range 1 through 12. Since you don't have AM/PM in your string, I suspect this was never what you wanted, and I wonder how the error went unnoticed. 小写字母hh表示AM或PM中的小时,范围为1到12。由于您的字符串中没有AM / PM,我怀疑这永远不是您想要的,而且我想知道错误是如何被忽略的。

Uppercase HH is for hour of day, 0 through 23: 大写HH是一天中的小时,从0到23:

public static final String DATE_FORMAT = "EEE MMM dd yyyy HH:mm:ss 'GMT'Z '('zzzz')'";

With this change both the old and the new way of parsing works on my computer. 通过此更改,旧的和新的解析方式都可以在我的计算机上使用。

When adding Locale.ENGLISH to both formatters, that is. Locale.ENGLISH添加到两个格式化程序时,就是这样。 You may want to do the same. 您可能想要做同样的事情。

The results I get are 我得到的结果是

Thu May 25 00:00:00 CEST 2017
2017-05-25T10:00+12:00[Pacific/Auckland]

Since CEST is 2 hours ahead of UTC, this is the same point in time, only rendered differently. 由于CEST比UTC早2小时,所以这是同一时间点,只是呈现方式有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM