In the following piece of code, I expected an exception to be thrown because the day of week doesn't match the day of month. I couldn't find anything in the DateTimeFormat Javadocs to say anything either way about being "strict" over such mis-matches, but I was confused that such bad data was allowed through. Have I misunderstood anything or is this a conscious design decision that I should deal with in some other way?
public static void main(String[] args) {
DateTimeFormatter DATE_FORMAT_LONG_DATE = //
DateTimeFormat.forPattern("EEEE, d MMMM yyyy")//
.withZone(DateTimeZone.forID("Australia/Melbourne"));
final String text = "Friday, 1 February 2016"; // Wrong.
final DateTime parsed = DATE_FORMAT_LONG_DATE.parseDateTime(text);
System.out.println(text);
System.out.println(DATE_FORMAT_LONG_DATE.print(parsed));
}
and output:
Friday, 1 February 2016
Friday, 5 February 2016
You're right, it does - as documented (emphasis mine):
Parsing builds up the resultant instant by 'setting' the value of each parsed field from largest to smallest onto an initial instant, typically 1970-01-01T00:00Z. This design means that day-of-month is set before day-of-week. As such, if both the day-of-month and day-of-week are parsed, and the day-of-week is incorrect, then the day-of-week overrides the day-of-month. This has a side effect if the input is not consistent.
I agree this is unfortunate - but you could always reformat afterwards and check whether the values are different.
See more on this question at Stackoverflow