DateTimeFormatter has a ResolverStyle that affects how strict or lenient the parser should be with invalid date and time values. To get an exception in this case you need to set the resolver style to STRICT.
You also need to use u
(year) instead of y
(year-of-era) in the format string.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu MMM dd")
.withResolverStyle(ResolverStyle.STRICT);
The default resolver type is SMART
:
Using smart resolution will perform the sensible default for each field, which may be the same as strict, the same as lenient, or a third behavior. Individual fields will interpret this differently.
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.