Java LocalDate parse pitfall

If you try to parse a String to a LocalDate with the parse method, you have to be carefull because the default ResolverStyle in the DateTimeFormater, when you call  the .ofPattern() method, is SMART.
Sounds funny, it is :).

public enum ResolverStyle {

    /**
     * Style to resolve dates and times strictly.
     * <p>
     * Using strict resolution will ensure that all parsed values are within
     * the outer range of valid values for the field. Individual fields may
     * be further processed for strictness.
     * <p>
     * For example, resolving year-month and day-of-month in the ISO calendar
     * system using strict mode will ensure that the day-of-month is valid
     * for the year-month, rejecting invalid values.
     */
    STRICT,
    /**
     * Style to resolve dates and times in a smart, or intelligent, manner.
     * <p>
     * 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.
     * <p>
     * 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.
     */
    SMART,
    /**
     * Style to resolve dates and times leniently.
     * <p>
     * Using lenient resolution will resolve the values in an appropriate
     * lenient manner. Individual fields will interpret this differently.
     * <p>
     * For example, lenient mode allows the month in the ISO calendar system
     * to be outside the range 1 to 12.
     * For example, month 15 is treated as being 3 months after month 12.
     */
    LENIENT;

}
ResolverStyle.java

Here are some examples what you should expect:

  • LENIENT – Style to resolve dates and times leniently.
2019-02-27  - is parsed to  - 2019-02-27
2019-02-28  - is parsed to  - 2019-02-28
2019-02-29  - is parsed to  - 2019-03-01  //Date moved to next month
  • SMART – Style to resolve dates and times in a smart, or intelligent, manner.
2019-02-27  - is parsed to  - 2019-02-27
2019-02-28  - is parsed to  - 2019-02-28
2019-02-29  - is parsed to  - 2019-02-28  //Date adjusted based on smart guessing
  • STRICT – Style to resolve dates and times strictly.
2019-02-27  - is parsed to  - 2019-02-27
2019-02-28  - is parsed to  - 2019-02-28
2019-02-29  - is parsed to  - //java.time.format.DateTimeParseException

For STRICT you have to use [uuuu] instead of [yyyy] for years.