java.time: 如何验证日历周
java.time: How to validate calendar weeks
如何验证日历周是否有效?
例如:
2018 年第 53 周在 ISO-8601 日历系统中无效。 2018年只有52个日历周
2020 年第 53 周有效 - 从 2020 年 12 月 28 日到 2021 年 1 月 3 日。("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)
如何通过 java.time
解决这个问题?
使用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
如何验证日历周是否有效?
例如:
2018 年第 53 周在 ISO-8601 日历系统中无效。 2018年只有52个日历周
2020 年第 53 周有效 - 从 2020 年 12 月 28 日到 2021 年 1 月 3 日。("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)
如何通过 java.time
解决这个问题?
使用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