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
You can do this with the temporal fields defined in WeekFields.ISO
.
First create a LocalDate
in the desired year, then adjust it to the first week, then adjust it to the first day of the week:
LocalDate localDate = LocalDate.ofYearDay(2020, 1);
System.out.println(
localDate
.with(WeekFields.ISO.weekOfWeekBasedYear(), 1)
.with(WeekFields.ISO.dayOfWeek(), 1)
);
Result:
2019-12-30