javajava-timeiso8601datetimeoffset

Why can’t Java 8 DateTimeFormatter.ISO_DATE_TIME handle offset zone without colon in it, ISO standards have 4 Offset formats.?


DateTimeFormatter formatter = new DateTimeFormatterBuilder()
    // date/time
    .append(DateTimeFormatter.ISO_DATE_TIME)
    .toFormatter();

//can't handle the first example

System.out.println(OffsetDateTime.parse("2022-03-17T23:00:00.000+0000", formatter));
System.out.println(OffsetDateTime.parse("2022-03-17T23:00:00.000+00", formatter));
System.out.println(OffsetDateTime.parse("2022-03-17T23:00:00.000+00:00", formatter));
System.out.println(OffsetDateTime.parse("2022-03-17T23:00:00.000Z", formatter));

Solution / work around is in Java 8 Date and Time: parse ISO 8601 string without colon in offset

I am checking why Java 8 ISO_DATE_TIME can't handle the format which is an ISO Complaint - https://en.wikipedia.org/wiki/ISO_8601#Time_zone_designators

<time>Z
<time>±hh:mm
<time>±hhmm
<time>±hh

My question is simple and straightforward -> Why is Java 8 not handling formats mentioned as part of ISO Standard ( only 4 ISO offset formats )?

https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_DATE_TIME

https://docs.oracle.com/javase/8/docs/api/java/time/ZoneOffset.html#getId--


Solution

  • The ISO-8601 standard has changed over the years. To my recollection, the offset without colon was only originally supported when using the basic ISO format where separators are omitted, eg. either use "2022-03-17T23:00:00.000+00:00" or "20220317T230000.000+0000" but not a mixture (2004 version of ISO-8601).

    For the method in question, java.time supports the common ISO format with separators. It was always the case that advanced use cases would need to create their own formatter, for which good support was designed. Key to the design of the method was the idea of round-tripping - the parse(String) method parses the output from toString() (unless using a specific formatter).