java怎样控制迭代周期_如何在Java 8中一年中每周迭代一次?

您可以使用以下内容构建日期并将其调整为一年中第一周的第一天:

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();

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值