您可以使用以下内容构建日期并将其调整为一年中第一周的第一天:
int year = 2016;
WeekFields weekFields = WeekFields.ISO;
LocalDate date = LocalDate.now().with(weekFields.weekBasedYear(), year)
.with(weekFields.weekOfWeekBasedYear(), 1)
.with(ChronoField.DAY_OF_WEEK, 1);
LocalDate date = LocalDate.now().with(IsoFields.WEEK_BASED_YEAR, year)
.with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 1)
.with(ChronoField.DAY_OF_WEEK, 1);
The ISO-8601 definition, where a week starts on Monday and the first week has a minimum of 4 days.
The ISO-8601 standard defines a calendar system based on weeks. It uses the week-based-year and week-of-week-based-year concepts to split up the passage of days instead of the standard year/month/day.
Note that the first week may start in the previous calendar year. Note also that the first few days of a calendar year may be in the week-based-year corresponding to the previous calendar year.
根据该定义,您可以获得:
This represents the concept of the year where weeks start on a fixed day-of-week, such as Monday and each week belongs to exactly one year.
在这种情况下,我们希望将其设置为所需年份.
> weekOfWeekBasedYear()代表以周为基础的一周
This represents the concept of the count of weeks within the year where weeks start on a fixed day-of-week, such as Monday and each week belongs to exactly one year.
在这种情况下,我们想要以周为基础的第一周,因此我们将其设置为1.
> ChronoField.DAY_OF_WEEK代表星期几.在这种情况下,我们想要一周的第一天,所以我们设置为1.
然后,有了这样一个日期,你可以通过调用LocalDate.plusWeeks(1)来迭代一年中的所有星期.问题是:你需要迭代多少次?一年可能超过52周.基于一周的年份有52或53周.
您可以使用以下内容检索周数.此调用rangeRefinedBy(date)检索给定日期的年周字段的有效值,并获得其最大值.
long maxWeekOfYear = weekFields.weekOfWeekBasedYear().rangeRefinedBy(date).getMaximum();