Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Java 8 DateTimeFormatter allows an incorrect month value in ResolverStyle.STRICT mode?

Tags:

People also ask

How do I use DateTimeFormatter with date?

DateTimeFormatter fmt = DateTimeFormatter. ofPattern("yyyy-MM-dd'T'HH:mm:ss"); System. out. println(ldt.

Is Java Time format DateTimeFormatter thread-safe?

DateTimeFormatter is a formatter that is used to print and parse date-time objects. It has been introduced in Java 8. DateTimeFormatter is immutable and thread-safe.

How do I format a date in Java 8?

Java 8 provides APIs for the easy formatting of Date and Time: LocalDateTime localDateTime = LocalDateTime. of(2015, Month. JANUARY, 25, 6, 30);


Why does this test pass, while the month value is obviously invalid (13)?

@Test
public void test() {

    String format = "uuuuMM";

    String value = "201713";

     DateTimeFormatter.ofPattern(format).withResolverStyle(ResolverStyle.STRICT)
        .parse(value);

}

When using a temporal query, the expected DateTimeParseException is thrown:

DateTimeFormatter.ofPattern(format).withResolverStyle(ResolverStyle.STRICT)
    .parse(value, YearMonth::from);

What happens when no TemporalQuery is specified?


EDIT: the 13 value seems to be a special one, as I learned thanks to the answer of ΦXocę 웃 Пepeúpa ツ (see Undecimber).

But the exception is not thrown even with another value, like 50:

@Test
public void test() {

    String format = "uuuuMM";

    String value = "201750";

     DateTimeFormatter.ofPattern(format).withResolverStyle(ResolverStyle.STRICT)
        .parse(value);

}