问题描述
我正在尝试获取特定期间的每月重复日期。除了我指定的重复日期为30以外,下面的代码有效,然后生成以下错误,这是正常的:
线程“ main”中的异常java.time.DateTimeException:无效的日期“ FEBRUARY 30”
我的问题是:如果指定的重复日期(天)高于该月的最后一天,是否可以将重复周期设置为该月的最后一天?
谢谢!
public class MonthlyRecurrence {
public static void main(String[] args) {
LocalDate startDate = LocalDate.of(2020,10,6);
LocalDate endDate = LocalDate.of(2021,12,31);
int dayOfMonth = 30;
List<LocalDate> reportDates = getReportDates(startDate,endDate,dayOfMonth);
System.out.println(reportDates);
}
private static List<LocalDate> getReportDates(LocalDate startDate,LocalDate endDate,int dayOfMonth) {
List<LocalDate> dates = new ArrayList<>();
LocalDate reportDate = startDate;
while (reportDate.isBefore(endDate)) {
reportDate = reportDate.plusMonths(1).withDayOfMonth(dayOfMonth);
dates.add(reportDate);
}
return dates;
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)