LocalDate initial = LocalDate.now();
DayOfWeek dayOfWeek = DayOfWeek.WEDNESDAY;
WeekFields weekFields = WeekFields.of(dayOfWeek, 1);
int weekNo = date.get(weekFields.weekOfWeekBasedYear());
System.out.println("Week No"+weekNo);

我在 2018 年 7 月 29 日使用上述代码。我预计第 30 周,但我得到 31。

为了得到 30 的结果,我在这里错过了什么?

最佳答案

如果根据 ISO-8601 预期输出,其中 current weekweek 30 ,则需要遵循以下步骤:



这是由 WeekFields.ISO 实现的。

相反,如果您希望一周从 WEDNESDAY 开始,则只需将 minimalDaysInFirstWeek1 更改为 4 (='First 4-day week'):

LocalDate date = LocalDate.now();
WeekFields weekFields = WeekFields.of(DayOfWeek.WEDNESDAY, 4);
int weekNo = date.get(weekFields.weekOfWeekBasedYear());
System.out.println("Week No " + weekNo);

关于java - 获取某个日期的周数(从星期三开始的一周),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51578704/

10-10 12:36