I currently have a Joda date parser that uses the DateTimeFormatterBuilder with half a dozen different date formats that I may receive.
I\'m migrating to Java 8\'s D
Here is what I eventually came up with. It handles three different major formats, each with multiple minor formatting differences (such as allowing either - or / delimiters) as well as handling variable number of microseconds (from 0 to 6):
private static final DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern( "[ddMMMyyyy:HH:mm:ss" )
.optionalStart()
.appendFraction( ChronoField.MICRO_OF_SECOND , 1 , 6 , true )
.optionalEnd()
.appendPattern( "[ ][Z][X]]" )
.appendPattern( "[yyyy[-][/]MM[-][/]dd['T'][ ]HH:mm[:][.]ss" )
.optionalStart()
.appendFraction( ChronoField.MICRO_OF_SECOND , 1 , 6 , true )
.optionalEnd()
.appendPattern( "[Z][X]]" )
.appendPattern( "[EEE, dd MMM yyyy HH:mm:ss zzz]" )
.toFormatter();