Java每月重复
我试图得到一个特定时期的每月重现日期。以下代码有效,除非我指定的重现日期为30。然后生成以下错误,这是正常的: 线程“main”java.time.DateTimeException中的异常:无效日期“2月30日” 我的问题是:如果指定的定期日期(天)高于每月的最后一天,是否有办法将定期设置为每月的最后一天 谢谢Java每月重复,java,java-time,recurrence,localdate,Java,Java Time,Recurrence,Localdate,我试图得到一个特定时期的每月重现日期。以下代码有效,除非我指定的重现日期为30。然后生成以下错误,这是正常的: 线程“main”java.time.DateTimeException中的异常:无效日期“2月30日” 我的问题是:如果指定的定期日期(天)高于每月的最后一天,是否有办法将定期设置为每月的最后一天 谢谢 public class MonthlyRecurrence { public static void main(String[] args) { Local
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;
}
}
公共类月重现{
公共静态void main(字符串[]args){
LocalDate startDate=LocalDate.of(2020,10,6);
LocalDate endDate=LocalDate.of(2021,12,31);
int dayOfMonth=30;
List reportDates=getReportDates(开始日期、结束日期、每月的日期);
系统输出打印项次(报告日期);
}
私有静态列表getReportDates(LocalDate startDate、LocalDate endDate、int dayOfMonth){
列表日期=新建ArrayList();
LocalDate reportDate=开始日期;
while(reportDate.isBefore(endDate)){
reportDate=reportDate.plusMonths(1)。带dayOfMonth(dayOfMonth);
日期。添加(报告日期);
}
返回日期;
}
}
无论月份的长度如何,您都需要指定一个日期来获取月份的特定日期。它作为第三个参数传递给您的方法
因为我不太确定你想要什么,所以我包括了两个调节器。一个用于特定的一天,另一个用于当月的最后一天。它们可以互换使用。但是,后者不会到达2021年12月31日,因为之前的排除了=
LocalDate startDate = LocalDate.of(2020, 10, 6);
LocalDate endDate = LocalDate.of(2021, 12, 31);
// Adjuster for a specific day
int dayOfMonth = 30; // must be final or effectively final
// as it is used in the following lambda.
TemporalAdjuster specificDay =
date-> {
LocalDate ld = (LocalDate)date;
int max = ld.getMonth().length(ld.isLeapYear());
int day = dayOfMonth > max ? max : dayOfMonth;
return date.with(ChronoField.DAY_OF_MONTH,day);
};
// Adjuster for the lastDay of the month.
TemporalAdjuster lastDay = TemporalAdjusters.lastDayOfMonth();
List<LocalDate> reportDates =
getReportDates(startDate, endDate, specificDay);
reportDates.forEach(System.out::println);
private static List<LocalDate> getReportDates(LocalDate startDate,
LocalDate endDate, TemporalAdjuster specificDay) {
List<LocalDate> dates = new ArrayList<>();
// round up start day to specificDay
LocalDate reportDate = startDate.with(specificDay);
while (reportDate.isBefore(endDate)) {
dates.add(reportDate);
reportDate = reportDate.plusMonths(1).with(specificDay);
}
return dates;
}
看看这个答案:(使用java.time方法)请提供更多细节。不确定您是否总是想要30号,或者日期是否相隔30天,等等。在提供更多指导之前,我选择将月的最后一天作为替代日期提供给您。我正在寻找具体的日期,您的答案完全解决了我的问题!非常感谢!
SpecificDay (30th) LastDayOfMonth
2020-10-30 2020-10-31
2020-11-30 2020-11-30
2020-12-30 2020-12-31
2021-01-30 2021-01-31
2021-02-28 2021-02-28
2021-03-30 2021-03-31
2021-04-30 2021-04-30
2021-05-30 2021-05-31
2021-06-30 2021-06-30
2021-07-30 2021-07-31
2021-08-30 2021-08-31
2021-09-30 2021-09-30
2021-10-30 2021-10-31
2021-11-30 2021-11-30
2021-12-30