This is due to the ResolverStyle
used by the formatter to parse the value. By default (at least on my machine) it's "smart":
For example, resolving year-month and day-of-month in the ISO calendar system using smart mode will ensure that the day-of-month is from 1 to 31, converting any value beyond the last valid day-of-month to be the last valid day-of-month.
... but you can make it "strict" instead, in which case the parsing fails. Complete example (using u
instead of y
to avoid the ambiguity of not specifying an era):
import java.time.*;
import java.time.format.*;
public class Test {
public static void main (String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M/d/uuuu");
// With the "smart" resolver style, it parses
System.out.println(formatter.getResolverStyle());
LocalDate date = LocalDate.parse("9/31/2018", formatter);
System.out.println(date);
// But with a strict style...
formatter = formatter.withResolverStyle(ResolverStyle.STRICT);
LocalDate.parse("9/31/2018", formatter);
}
}