【问题标题】:JodaTime: get date(s) between two dates using specific intervalJoda Time:使用特定间隔获取两个日期之间的日期)
【发布时间】: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”的一种或另一种方法。

标签: java jodatime


【解决方案1】:

如果你无法使用 Java8,请尝试使用 Google Guava:

    private static void getDays(DateTime todayDate, DateTime expiryDate, int interval) {
           int numberOfDays = Days.daysBetween(new LocalDate(todayDate), new LocalDate(expiryDate)).getDays();
           Range<Integer> open = Range.closed(1, numberOfDays/interval);
           ImmutableList<Integer> integers = ContiguousSet.create(open, DiscreteDomain.integers()).asList();
           FluentIterable.from(integers).transform(new ConvertToDate(interval)).toList().forEach(System.out::println);
}

private static class ConvertToDate implements Function<Integer, DateTime> {
    private final int interval;

    public ConvertToDate(int interval) {
        this.interval = interval;
    }

    @Override
    public DateTime apply(Integer integer) {
        return DateTime.now().plusDays(integer* interval);
    }
}

我不知道这是否是更好的解决方案,但我发现它更易于阅读。

【讨论】:

    【解决方案2】:

    你可以使用流

        int numberOfDays = Days.daysBetween(LocalDate.now(), LocalDate.now().plusDays(10)).getDays();
        List<LocalDate> collect = Stream.iterate(LocalDate.now(), e -> e.plusDays(1)).limit(numberOfDays).collect(Collectors.toList());
        collect.forEach(System.out::println);
    

    【讨论】:

    • 看起来正在做这项工作,虽然我不能在这个项目中使用 Java 8。
    猜你喜欢
    • 1970-01-01
    • 2011-04-17
    • 1970-01-01
    • 2014-03-24
    • 2016-04-18
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多