My program recognizes dates of various formats in a broken JSON and replaces them with ISO8601. Then the JSON is passed to a 3rd-party library.
One of the formats has no time and is supposed to be parsed as 00:00:00, but parsing a seemingly valid date string fails with Unable to obtain LocalTime from 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