I have the following tests.
@Test
void withoutColon_fails() {
ZonedDateTime.parse("2019-01-24T12:10:58.036820+0400");
}
@Test
void withColon_ok() {
ZonedDateTime.parse("2019-01-24T12:10:58.036820+04:00");
}
The only difference between the date-times I'm trying to parse is that in the first case there is no colon between hours and minutes in timezone designation, and in the second case there is a colon.
First of them fails:
java.time.format.DateTimeParseException: Text '2019-01-24T12:10:58.036820+0400' could not be parsed, unparsed text found at index 29
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2049)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
at java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:598)
at java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:583)
According to the javadoc, parse()
, through DateTimeFormatter.ISO_ZONED_DATE_TIME
, eventually uses DateTimeFormatter.ISO_OFFSET_DATE_TIME
, on which, in turn, we can read that it is
The ISO date-time formatter that formats or parses a date-time with an offset, such as '2011-12-03T10:15:30+01:00'.
Following the javadoc links further, we get to ZoneOffset.getId()
which says that it accepts:
Z - for UTC (ISO-8601)
+hh:mm or -hh:mm - if the seconds are zero (ISO-8601)
+hh:mm:ss or -hh:mm:ss - if the seconds are non-zero (not ISO-8601)
So, the colon is always mandatory, according to the javadocs.
But, according to https://en.wikipedia.org/wiki/ISO_8601 ,
The UTC offset is appended to the time in the same way that 'Z' was above, in the form ±[hh]:[mm], ±[hh][mm], or ±[hh].
So the colon is NOT mandatory, according to the standard, and it seems that Java does not support the standard completely.
The question is: why? Was the omission deliberate, is there a good reason for this?
This works this way in both Java 8 and Java 11.