How does Java LocalDate make the last week of the year the 53rd week instead of the first week of the new year?

How does Java LocalDate make the last week of the year the 53rd week instead of the first week of the new year? … here is a solution to the problem.

How does Java LocalDate make the last week of the year the 53rd week instead of the first week of the new year?

IF YOU THINK THAT THE WEEK WILL START ON JANUARY 1ST OF EACH YEAR, AND THE START OF THE WEEK IS SUNDAY, THEN 2019 WILL HAVE WILL BE 53 WEEKS.
Following the above Jan 29, 30, 31 2019 will enter Week-53 of 2019.

In the documentation for IsoFields gives the view for WEEK_OF_WEEK_ BASED_YEAR, all three fields are validated based on their range of valid values. The week-of-week-based-year field is validated from 1 to 52 or 53, depending on the week-based year.

So I assume the output of the following code should be: WEEK_OF_WEEK_BASED_YEAR 53 & WEEK_BASED_YEAR 2019.

But its output is: 1 & 2020

import java.time.LocalDate;
import java.time.chrono.IsoChronology;
import java.time.format.DateTimeFormatter;
import java.time.format.ResolverStyle;
import java.time.temporal.IsoFields;

public class WeekStartDemo {
    public static void main(String args[]) {
        DateTimeFormatter DATE_FORMATTER = DateTimeFormatter
                .ofPattern("uuuu-MM-dd")
                .withChronology(IsoChronology.INSTANCE)
                .withResolverStyle(ResolverStyle.STRICT);

LocalDate updatedDate = LocalDate.parse("2019-12-30", DATE_FORMATTER);

System.out.println(updatedDate.toString());

System.out.println(updatedDate.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR));
        System.out.println(updatedDate.get(IsoFields.WEEK_BASED_YEAR));
    }
}

If I pass the date as 2019-12-28, then it will return WEEK_OF_WEEK_BASED_YEAR 52 & WEEK_BASED_YEAR 2019 Top >. However, it does not apply to the last week of 2019 (i.e. week 53).

Let me know what I’m missing in the code above.

Solution

As I mentioned in the comments, as well as from >IsoFields For Javadoc links, week-based years are themselves defined relative to standard ISO proptic years. It differs from standard years in that it always starts on Monday, not Sunday. Using the code you published, iterating from 1900 to 2300 and resolving the last WEEK_OF_WEEK_BASED_YEAR should be easy to find a year with 53 weeks and print the value of 53 for it. For example,

DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("uuuu-MM-dd")
        .withChronology(IsoChronology.INSTANCE)
        .withResolverStyle(ResolverStyle.STRICT);

for (int i = 1900; i < 2300; i++) {
    LocalDate updatedDate = LocalDate.parse(String.format("%d-12-31", i), DATE_FORMATTER);
    if (updatedDate.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR) == 53) {
        System.out.println(i);
    }
}

The first few values I get are

1903
1908
1914
1920
1925
1931
1936
1942

Skip a little….

2009
2015
2020
2026

So this year (2020) there are 53 weeks and 2019 there are not.

Related Problems and Solutions