Java DateTimeFormatter issue

Viewed 93

The following code (in main)

LocalDate d ;
DateTimeFormatter formatter;

d = LocalDate.of(2021, 11, 14);
System.out.println(d);

formatter = DateTimeFormatter.ofPattern("MM.dd.YYYY");
System.out.println(formatter.format(d));

formatter = DateTimeFormatter.ofPattern("YYYY/MM/dd");
System.out.println(formatter.format(d)+ "\n");
            
d = LocalDate.of(2021, 1, 1);
System.out.println(d);
                
formatter = DateTimeFormatter.ofPattern("MM.dd.YYYY");
System.out.println(formatter.format(d));

formatter = DateTimeFormatter.ofPattern("YYYY/MM/dd");
System.out.println(formatter.format(d));

outputs in my Netbeans 12.5 with the latest or fairly recent Oracle Java 17

run:
2021-11-14
14.11.2021
2021/11/14

2021-01-01
01.01.2020
2020/01/01
BUILD SUCCESSFUL (total time: 0 seconds)

The formatted output for today, 14-NOV-2021, is ok, but output for 01-JAN-2021 is wrong. Formatted output prints 2020 , one year off.

Can you repeat this? If so: Any ideas why this happens?

2 Answers

According to the Javadoc of java.time.format.DateTimeFormatter

  • y (lowercase) represents the year-of-era and
  • Y (uppercase) represents the week-based-year.

The week-based date/time system is defined in ISO 8601 (see: Wikipedia article).

You want u, not Y:

  • Y is week-based year. If you're an accountant, this is important. If you're not, this is the devil: It seems right almost always, except in certain highly exotic dates, such as jan 1st. Weeks customarily are defined as: "Whatever year it is on the thursday of that week, that's the year that week belongs to", so if e.g. Jan 1st falls on a friday, then the week that contains Jan 1st is of the previous year (as the thursday of that week was dec 31st).

  • y is year as you mostly know it, except it doesn't really do what you want when go to BC years. It's not likely to come up, but then neither is the week-based-year thing. Nobody likes Year-2k style bugs, right?

  • u truly does what you imagined. It just gives you the year. Negative if need be.

Related