LocalDateTime now = LocalDateTime.of(2022,12,31,0,1,1);
WeekFields weekFields = WeekFields.of(DayOfWeek.FRIDAY,1);
int dayOfWeek = now.getDayOfWeek().get(weekFields.dayOfWeek());
System.out.println("日期: "+ LocalDateTimeUtil.format(now, DatePattern.NORM_DATETIME_PATTERN));
System.out.println("第几周weekOfYear: "+now.get(weekFields.weekOfYear()));
System.out.println("第几周weekOfWeekBasedYear: "+now.get(weekFields.weekOfWeekBasedYear()));
System.out.println("本周的第几天(从1开始): "+dayOfWeek);
System.out.println("本周的开始日期: "+now.minusDays(dayOfWeek - 1));
System.out.println();
now = now.plusDays(1);
dayOfWeek = now.getDayOfWeek().get(weekFields.dayOfWeek());
System.out.println("加一天日期: "+ LocalDateTimeUtil.format(now, DatePattern.NORM_DATETIME_PATTERN));
System.out.println("第几周weekOfYear: "+now.get(weekFields.weekOfYear()));
System.out.println("第几周weekOfWeekBasedYear: "+now.get(weekFields.weekOfYear()));
System.out.println("本周的第几天(从1开始): "+dayOfWeek);
System.out.println("本周的开始日期: "+now.minusDays(dayOfWeek - 1));
System.out.println();
now = now.plusWeeks(1);
dayOfWeek = now.getDayOfWeek().get(weekFields.dayOfWeek());
System.out.println("加一周日期: "+ LocalDateTimeUtil.format(now, DatePattern.NORM_DATETIME_PATTERN));
System.out.println("第几周weekOfYear: "+now.get(weekFields.weekOfYear()));
System.out.println("第几周weekOfWeekBasedYear: "+now.get(weekFields.weekOfYear()));
System.out.println("本周的第几天(从1开始): "+dayOfWeek);
System.out.println("本周的开始日期: "+now.minusDays(dayOfWeek - 1));
- 每个周的周期是开始日定义
- 表示一个周的周期是从周一开始的: WeekFields weekFields = WeekFields.ISO;
- 你也可以自定你自子想要的,表示一个周的周期是从周五开始的。其中参数一表示从周几开始,参数二表示一个周的最小天数: WeekFields weekFields = WeekFields.of(DayOfWeek.FRIDAY,1);
- 如果一周涉及到了跨年,如2022.12.31是周六,2023.01.01是周六日,这涉及到了年份的变化。
- weekOfWeekBasedYear会把2022.12.31、2023.01.01 看做是2021年的第一周, 结果都是=1;
- weekOfYear则会把2021.12.31看做是2022年的最后一周,又会吧2023.01.01看最是2023年的第一周,即当做两周, 当用2021.12.31日计算的时候结果=53, 使用2023.01.01去计算的时候结果=1