If this function is passed an invalid date and an IllegalFieldValueExcption is thrown I would like to return a default date of 0001-01-01. But when the code below is ran the date that is created in defaultDate is 0001-01-02, which is wrong. Does anyone see anything i’m doing wrong?
public Date unmarshal(String string) {
DateTimeFormatter fmt = new DateTimeFormatter(null, DateTimeFormat.forPattern("yyyy-MM-dd").getParser());
Date defaultDate = DateTime.parse("0001-01-01", fmt).toDate();
try {
return formatter.parseDateTime(string).toDate();
}
catch (IllegalFieldValueException e) {
return defaultDate;
}
}
EDIT:
I updated the first two lines with these and it is working perfect now, thanks!
Chronology chrono = GJChronology.getInstance();
Date defaultDate = new DateTime(0001, 01, 01, 10, 0, 0, 0, chrono).toDate();
The issue you have is because the default chronology in Joda-Time is ISO. This calendar system is the same as that used by business in the majority of the world today. The ISO system is unsuitable for historical work before 1583 as it applies the leap year rules from today back in time (it is a proleptic calendar).
So you shoudn’t use a
defaultDatebefore 1583 with ISO Chronology. If you need to use such Date, you have to use a different chronology in Joda-Time.