解析日期时无法从TemporalAccessor获取ZonedDateTime

asm*_*ier 3 java datetime java-8 java-time

使用Java 1.8.0_51以下代码(取自Unable从TemporalAccessor获取OffsetDateTime)

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd").withZone(ZoneId.of("Europe/Berlin"));
OffsetDateTime offsetDateTime = ZonedDateTime.parse("20151113", formatter).toOffsetDateTime();
System.out.println(offsetDateTime.format(DateTimeFormatter.ISO_DATE));
Run Code Online (Sandbox Code Playgroud)

抛出异常:

java.time.format.DateTimeParseException: Text '20151113' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO,Europe/Berlin resolved to 2015-11-13 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)
Run Code Online (Sandbox Code Playgroud)

这次我做错了什么?

Tun*_*aki 6

你忘了设定时间.

如果您将我的答案与您的代码进行比较,您会发现唯一的区别是缺少时间信息.A ZonedDateTime包含时间信息,由于您当前的格式化程序无法处理它,ZonedDateTime因此无法形成实例.

您也可以在包含的堆栈跟踪中看到它

Caused by: java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: {},ISO,Europe/Berlin resolved to 2015-11-13 of type java.time.format.Parsed
    at java.time.LocalTime.from(LocalTime.java:409)
    at java.time.ZonedDateTime.from(ZonedDateTime.java:560)
    ... 5 more
Run Code Online (Sandbox Code Playgroud)

根据您的需要,您可以构建自定义格式化程序,DateTimeFormatterBuilder并调用parseDefaulting以为每个时间字段提供默认值.如果要默认为午夜,则可以设置NANO_OF_DAY为0.示例示例

public static void main(String[] args) {
    DateTimeFormatter formatter = 
        new DateTimeFormatterBuilder().appendPattern("yyyyMMdd")
                                      .parseDefaulting(ChronoField.NANO_OF_DAY, 0)
                                      .toFormatter()
                                      .withZone(ZoneId.of("Europe/Berlin"));

    OffsetDateTime offsetDateTime = ZonedDateTime.parse("20151113", formatter).toOffsetDateTime();
    System.out.println(offsetDateTime.format(DateTimeFormatter.ISO_DATE));
}
Run Code Online (Sandbox Code Playgroud)

另一种可能的解决方案是将文本解析为a LocalDate,然后ZoneDateTime用它构造一个:

public static void main(String[] args) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
    LocalDate parsed = LocalDate.parse("20151113", formatter);
    ZonedDateTime zonedDateTime = ZonedDateTime.of(parsed, LocalTime.MIDNIGHT, ZoneId.of("Europe/Berlin"));
    // get OffsetDateTime similarly
}
Run Code Online (Sandbox Code Playgroud)