javajava-timeiso8601

java - get the first monday in ISO year


I want to get the day on which the first Monday of a specific ISO year will be.

I have an int variable representing the year, and I want to compute a LocalDate representing the first Monday in that ISO year (following ISO 8601). I'm referring to the following concept: First week (ISO week date). So the Monday of the first ISO week can be in December.

Is there an elegant way to do this, similar to the answer of the following question? Get the first Monday of a month


Solution

  • Corrected implementation (see my comment to the other answer):

    public static LocalDate getMondayOfFirstIsoWeek(int year) {
      // January 4 is always within ISO Week 1 of the given year
      LocalDate jan4 = LocalDate.of(year, 1, 4); 
      // Get the Monday of the ISO week
      return jan4.with(DayOfWeek.MONDAY);
    }