/**
* @Author: chuxia0811
* @Date: 2022/7/16 14:55
* @Description :
*/
public class Lasted_30day {
public static void main(String[] args) {
LocalDate localDate = LocalDate.now().minusDays(-1);
LocalDate afterDate = localDate.minusDays(30);
//获取倒数30天
List<LocalDate> localDates = new ArrayList<>(30);
for (LocalDate currentdate = afterDate;
currentdate.isBefore(localDate) ||
currentdate.isEqual(localDate);
currentdate = currentdate.plusDays(1)) {
localDates.add(LocalDate.of(currentdate.getYear(), currentdate.getMonth(), currentdate.getDayOfMonth()));
}
// 倒叙排列近30天日期
localDates = localDates.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
System.out.println(localDates.toString());
}
}
运行结果如下:
[2022-07-17, 2022-07-16, 2022-07-15, 2022-07-14, 2022-07-13, 2022-07-12, 2022-07-11, 2022-07-10, 2022-07-09, 2022-07-08, 2022-07-07, 2022-07-06, 2022-07-05, 2022-07-04, 2022-07-03, 2022-07-02, 2022-07-01, 2022-06-30, 2022-06-29, 2022-06-28, 2022-06-27, 2022-06-26, 2022-06-25, 2022-06-24, 2022-06-23, 2022-06-22, 2022-06-21, 2022-06-20, 2022-06-19, 2022-06-18, 2022-06-17]