Java 8 DateTimeFormatter parsing optional sections
问题 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