更新时间:2023-09-19 15:19:16
If you are using Java 8 you can use java.time API Like so :
int addWeek = 0;
if(startDate.get(WeekFields.ISO.weekOfYear()) < endDate.get(WeekFields.ISO.weekOfYear())){
addWeek = 1;
}
long weeks = WEEKS.between(startDate, endDate) + addWeek;//Get the number of weeks in your case (52)
List<Integer> numberWeeks = new ArrayList<>();
if (weeks >= 0) {
int week = 0;
do {
//Get the number of week
int weekNumber = startDate.plusWeeks(week).get(WeekFields.ISO.weekOfYear());
numberWeeks.add(weekNumber);
week++;
} while (week <= weeks);
}
[34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34]
请注意,您会同时获得两个年份的两个星期编号,即 2018 [34-52]
的几周,然后是 2019 [1-33]
Note that you get both week numbers from both years, weeks of 2018 [34-52]
, then weeks of 2019 [1-33]