我的程序在一个中断的JSON中识别各种格式的日期,并用ISO8601替换它们。然后JSON被传递给第三方库。

从 TemporalAccessor 获取本地时间
public static void main(String[] args) throws Exception {
    DateTimeFormatter standardFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX", Locale.US).withZone(ZoneOffset.UTC );
    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.US).withZone(ZoneOffset.UTC );
    ZonedDateTime utcDateTime = ZonedDateTime.parse("2022-11-23" , df);
    System.out.println( utcDateTime.format(standardFormatter) );
    
}

_

Exception in thread "main" java.time.format.DateTimeParseException: Text '2022-11-23' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO,Z resolved to 2022-11-23 of type java.time.format.Parsed
    at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2017)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1952)
    at java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:598)
    at bookproposal.actions.FixupDates.main(FixupDates.java:107)
Caused by: java.time.DateTimeException: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO,Z resolved to 2022-11-23 of type java.time.format.Parsed
    at java.base/java.time.ZonedDateTime.from(ZonedDateTime.java:566)
    at java.base/java.time.format.Parsed.query(Parsed.java:235)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
    ... 2 more
Caused by: java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: {},ISO,Z resolved to 2022-11-23 of type java.time.format.Parsed
    at java.base/java.time.LocalTime.from(LocalTime.java:431)
    at java.base/java.time.ZonedDateTime.from(ZonedDateTime.java:561)
    ... 4 more