When you use a pattern like
DateTimeFormat.forPattern("MMddyyyy")
it can read a date like "120100"
. What it does is read the 12
for MM
, the 01
for dd
, and 00
as a literal value for yyyy
, ie. 0
starting at year 0
, so 0000
.
Because that pattern appears before the MMddyy
in your parsers
array, it will be used to parse your date string.
What you'll want to do, along with reordering or removing that format, is use a pivot year.
public static void main(String[] args) throws Exception {
String str = "120100";
DateTimeParser[] formats = {
DateTimeFormat.forPattern("MM/dd/yyyy").getParser(),
DateTimeFormat.forPattern("MM/dd/yy").getParser(),
DateTimeFormat.forPattern("MM-dd-yyyy").getParser(),
DateTimeFormat.forPattern("MM-dd-yy").getParser(),
DateTimeFormat.forPattern("MMddyy").getParser()
};
DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, formats).toFormatter()
.withPivotYear(1950).withLocale(Locale.US);
DateTime dateTime = formatter.parseDateTime(str);
System.out.println(dateTime);
}
prints
1900-12-01T00:00:00.000-05:00
I'm still trying to figure out how appendTwoDigitYear
works.