所以我希望这段代码可以在新的 Java 8 日期/时间包下工作,因为它所做的只是使用相同的内置 DateTimeFormatter 实例 (ISO_INSTANT) 将给定的 ZonedDateTime 转换为字符串并返回:
ZonedDateTime now = ZonedDateTime.now();
System.out.println(ZonedDateTime.parse(
now.format(DateTimeFormatter.ISO_INSTANT),
DateTimeFormatter.ISO_INSTANT));
但显然它没有:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2014-09-01T19:37:48.549Z' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {MilliOfSecond=549, NanoOfSecond=549000000, MicroOfSecond=549000, InstantSeconds=1409600268},ISO of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853)
at java.time.ZonedDateTime.parse(ZonedDateTime.java:597)
我已经看过这个条目,但它对我没有帮助,因为需要 ZonedDateTime 对象而不是本地对象,还因为我已经安装了 8u20:Unable to get ZonedDateTime from TemporalAccessor using DateTimeFormatter and ZonedDateTime in Java 8
有人知道这里发生了什么吗?