I need to parse date-times as strings coming as two different formats:
- 19861221235959Z
- 1986-12-21T23:59:59Z
The following dateTimeFormatter pattern properly parses the first kind of date strings
DateTimeFormatter.ofPattern ("uuuuMMddHHmmss[,S][.S]X")
but fails on the second one as dashes, colons and T are not expected.
My attempt was to use optional sections as follows:
DateTimeFormatter.ofPattern ("uuuu[-]MM[-]dd['T']HH[:]mm[:]ss[,S][.S]X")
Unexpectedly, this parses the second kind of date strings (the one with dashes), but not the first kind, throwing a
java.time.format.DateTimeParseException: Text '19861221235959Z' could not be parsed at index 0
It's as if optional sections are not being evaluated as optional...