1.获取传入日期区间中所有日期
1.代码演示
@Test
public static List<LocalDate> getAllDatesInTheDateRange(LocalDate startDate, LocalDate endDate) {
List<LocalDate> localDateList = new ArrayList<>();
if (startDate.isAfter(endDate)) {
return null;
}
while (startDate.isBefore(endDate)) {
localDateList.add(startDate);
startDate = startDate.plusDays(1);
}
localDateList.add(endDate);
return localDateList;
}
2.结果验证
public static void main(String[] args) {
List<LocalDate> localDateList = getAllDatesInTheDateRange(LocalDate.of(2022, 2, 1), LocalDate.of(2022, 2, 28));
if (CollectionUtils.isNotEmpty(localDateList)) {
localDateList.forEach(System.out::println);
}
2022-02-01
2022-02-02
2022-02-03
2022-02-04
2022-02-05
2022-02-06
2022-02-07
2022-02-08
2022-02-09
2022-02-10
2022-02-11
2022-02-12
2022-02-13
2022-02-14
2022-02-15
2022-02-16
2022-02-17
2022-02-18
2022-02-19
2022-02-20
2022-02-21
2022-02-22
2022-02-23
2022-02-24
2022-02-25
2022-02-26
2022-02-27
2022-02-28