We try to parse the following ISO 8601 DateTime String with timezone offset:
final String input = "2022-03-17T23:00:00.000+0000";
OffsetDateTime.parse(input);
LocalDateTime.parse(input, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
Both approaches fail (which makes sense as OffsetDateTime
also use the DateTimeFormatter.ISO_OFFSET_DATE_TIME
) because of the colon in the timezone offset.
java.time.format.DateTimeParseException: Text '2022-03-17T23:00:00.000+0000' could not be parsed at index 23
But according to Wikipedia there are 4 valid formats for a timezone offset:
<time>Z
<time>±hh:mm
<time>±hhmm
<time>±hh
Other frameworks/languages can parse this string without any issues, e.g. the Javascript Date()
or Jacksons ISO8601Utils
(they discuss this issue here)
Now we could write our own DateTimeFormatter
with a complex RegEx, but in my opinion the java.time
library should be able to parse this valid ISO 8601 string by default as it is a valid one.
For now we use Jacksons ISO8601DateFormat
, but we would prefer to use the official date.time
library to work with. What would be your approach to tackle this issue?
ISO_OFFSET_DATETIME
requires the time colons and the date separators too, which aren't required by ISO-8601. Basically, it's pretty strict, unfortunately. – Tinput = input.replaceFirst("\+\d\d", "$0:")
before parsing the date. – StoatISO_OFFSET_DATE_TIME
doesn't accept "just basic" either - the example of "19850412T101530+04" in ISO-8601 fails to parse. There's aBASIC_ISO_DATE
formatter, but noBASIC_ISO_OFFSET_DATE_TIME
or a "just give me either valid format" formatter. – TDateTimeFormatter.ofPattern( "uuuu-MM-dd'T'HH:mm:ss.SSSX" )
– Batik2018-08-26T15:00:00+01
, but not2018-08-26T15:00:00+0100
. Tested with OpenJDK 11. – Abnormality2018-08-26T15:00:00+0100
is not valid ISO-8601, this would not be a bug. You probably mean2018-08-26T15:00:00+01:00
(extended) or20180826T150000+0100
(basic) – Villarreal