Reputation: 722
How to verify that a calendar week is valid?
For example:
Week 53 in the week based year 2018 is not valid in the ISO-8601 calendar system. The year 2018 only has 52 calendar weeks.
Week 53 in 2020 is valid - it is from 28. Dec 2020 until 03. Jan 2021. ("The first week of a week-based-year is the first Monday-based week of the standard ISO year that has at least 4 days in the new year" - see IsoFields javadoc)
How to figure this out via java.time
?
Upvotes: 1
Views: 315
Reputation: 63385
Use TemporalAccessor.range(TemporalField)
:
LocalDate date = LocalDate.of(2020, 6, 1);
ValueRange range = date.range(IsoFields.WEEK_OF_WEEK_BASED_YEAR);
System.out.println(range.getMaximum()); // 53
Upvotes: 4