【发布时间】:2017-04-14 18:23:53
【问题描述】:
使用 JodaTime,我目前正在检查开始日期和结束日期之间的天数,如下所示:
todayDate = new DateTime();
expiryDate = new DateTime(dbReportModel.getValidityTime());
lastDate = new DateTime(dbReportModel.getLastDate());
Days dayDifference = Days.daysBetween(new LocalDate(todayDate), new LocalDate(expiryDate));
如果上述日期差异小于或等于某些 int 天数,我会开始通知我的用户:
if (dayDifference.getDays() <= expiryNotificationProperties.getDaysBefore())
程序每天运行。问题是我不需要每天都发送信息。我只需要通知每个 f.e. 15天。
目前我有这种方法可以使用间隔获取在给定开始日期和结束日期之间的日期列表:
private List<LocalDate> getDatesBetweenExpiryDateTodayDate (DateTime todayDate, DateTime expiryDate, int interval) {
List<LocalDate> listOfDatesWithInterval = new ArrayList <>();
int numberOfDays = Days.daysBetween(new LocalDate(todayDate), new LocalDate(expiryDate)).getDays();
LocalDate dynamicExpiryDate = expiryDate.toLocalDate();
while (numberOfDays > 0) {
listOfDatesWithInterval.add(dynamicExpiryDate.minusDays(interval));
dynamicExpiryDate = dynamicExpiryDate.minusDays(interval);
numberOfDays -= interval;
}
return listOfDatesWithInterval;
}
这是对今天的日期是否在列表中的附加检查:
List<LocalDate> listOfDatesUsingInterval = getDatesBetweenExpiryDateTodayDate(todayDate, expiryDate, interval);
if (listOfDatesUsingInterval.contains(todayDate.toLocalDate())) {
是否有可能在 JodaTime 中以更好的方式做到这一点?是否有内置功能来实现此结果?
【问题讨论】:
-
为什么不起作用?
-
@lexicore 它有效。我已经编辑了帖子。有没有更好的方法来实现这样的功能?
-
我认为没有更好的方法。该 API 不提供此类功能 OOTB,您将采用“startDate + dayIndex”的一种或另一种方法。