简体   繁体   中英

JodaTime: get date(s) between two dates using specific interval

With JodaTime I am currently checking the amount of days between start date and end date like this:

todayDate = new DateTime();
expiryDate = new DateTime(dbReportModel.getValidityTime());
lastDate = new DateTime(dbReportModel.getLastDate());

Days dayDifference = Days.daysBetween(new LocalDate(todayDate), new LocalDate(expiryDate));

I start to notify my users about something if the above day difference is less or equal that some int of days:

if (dayDifference.getDays() <= expiryNotificationProperties.getDaysBefore())

The program is ran every day. The problem is that I do not need to send out information every day. I only need to notify every fe 15 days.

Currently I have this method to get a list of dates that are in between the given start and end date using the interval:

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;
    }

And this is the additional check that today's date is in the list:

List<LocalDate> listOfDatesUsingInterval = getDatesBetweenExpiryDateTodayDate(todayDate, expiryDate, interval);
if (listOfDatesUsingInterval.contains(todayDate.toLocalDate())) {

Is it possible to do this in JodaTime in perhaps a better manner? Is there built in functionality to achieve this result?

If you could not use Java8 try to use 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);
    }
}

I don't know if it is better solution but I find it is easier to read.

You can use streams

    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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM