30天
LocalDate localDate = LocalDate.now();
LocalDate afterDate = localDate.minusDays(30);
// 获取倒数30天
List<LocalDateTime> localDateTimes = new ArrayList<>(30);
for (LocalDate currentdate = afterDate; currentdate.isBefore(localDate) || currentdate.isEqual(localDate); currentdate = currentdate.plusDays(1)) {
localDateTimes.add(LocalDateTime.of(currentdate.getYear(), currentdate.getMonth(), currentdate.getDayOfMonth(), 0, 0));
}
12个月
LocalDate localDate = LocalDate.now();
LocalDate afterDate = localDate.minusMonths(12);
// 获取倒数12个月
List<LocalDateTime> localDateTimes = new ArrayList<>(30);
for (LocalDate currentdate = afterDate; currentdate.isBefore(localDate) || currentdate.isEqual(localDate); currentdate = currentdate.plusMonths(1)) {
localDateTimes.add(LocalDateTime.of(currentdate.getYear(), currentdate.getMonth(), 1, 0, 0));
}
本文介绍如何使用Java中的LocalDate和LocalDateTime类来获取指定时间段内的日期集合,包括过去30天和12个月的具体实现。
2万+

被折叠的 条评论
为什么被折叠?



