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...
19861221235959
appears to be the year. It doesn't stop at 4 digits when parsing, only has a 4 digit minimum when formatting. – Illustrious19861221235959
is too large to be a year so it fails to parse it. – Illustrious19861221T235959Z
. – Colostrum