Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this quick tutorial, we’ll study several ways to iterate over a range of dates, using a start and end date, in Java 7, Java 8, and Java 9.

2. Java 7

Starting with Java 7, we’ll use the class java.util.Date for holding the date values and java.util.Calendar for incrementing from one date to the next.

Let’s see an example using a simple while loop along with the java.util.Date and java.util.Calendar classes:

void iterateBetweenDatesJava7(Date start, Date end) {
    Date current = start;

    while (current.before(end)) {
        processDate(current);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(current);
        calendar.add(Calendar.DATE, 1);
        current = calendar.getTime();
    }
}

3. Java 8

Starting with Java 8, we can use the new Java 8 Date API. This gives us self-handling, immutable, fluent, and thread-safe objects. It also allows us to write cleaner code without the need for a helper class like java.util.Calendar for incrementing the dates.

Let’s use a simple for loop, the LocalDate class, and the method plusDays(1) to move forward through the range of dates:

void iterateBetweenDatesJava8(LocalDate start, LocalDate end) {
    for (LocalDate date = start; date.isBefore(end); date = date.plusDays(1)) {
        processDate(date);
    }
}

It’s worth noting here that although the Stream API is available beginning in Java 8, iterating between two dates using the Date API in conjunction with the Stream API isn’t possible until Java 9.

Please check out this article for a more detailed explanation of the Java 8 Date API.

4. Java 9+

Java 9 introduces the method datesUntil, which lets us use the Stream API to iterate from start date to end date.

Let’s update our example code to take advantage of this feature:

void iterateBetweenDatesJava9(LocalDate start, LocalDate end) {
    start.datesUntil(end).forEach(this::processDate);
}

5. Conclusion

As we’ve seen in this quick article, iterating over a range of dates is a simple task in Java. This is especially true when using Java 8 and later, where we can handle dates more easily with the Date API.

Note that in Java 7 and earlier, it’s recommended to deal with both date and time, even if we’re using only dates.

However, with Java 8 and later, we have the advantage of choosing an appropriate class from the Date API like LocalDate, LocalDateTime, and other options, according to our needs.

And of course, starting in Java 9, we can use the Stream API in conjunction with the Date API to iterate a stream of dates.

As always, the code snippets can be found over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.