Java Get List of All Days Between Two ZonedDateTime

Tags: Java ZonedDateTime Java 8 DateUtil

In this Java tutorial, we learn how to write Java program to get the list of all days between two ZonedDateTime values and return it as a List of ZonedDateTime in Java programming language.

How to get all days between two ZonedDateTime objects

At this step, create a new Java class named DateUtil, and implement a static method named getAllDaysBetween(ZonedDateTime start, ZonedDateTime end) which return a List of ZonedDateTime values of all days between start and end as the following Java code.

DateUtil.java

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;

public class DateUtil {

    /**
     * Get list of all days between two ZonedDateTime values and return it as list of ZonedDateTime
     * @param start the start ZonedDateTime object
     * @param end the end ZonedDateTime object
     * @return the list of all ZonedDateTime objects between start and end
     */
    public static List<ZonedDateTime> getAllDaysBetween(ZonedDateTime start, ZonedDateTime end) {
        List<ZonedDateTime> result = new ArrayList<>();
        LocalDate startLocalDate = start.toLocalDate();
        LocalDate endLocalDate = end.toLocalDate();
        while(startLocalDate.isBefore(endLocalDate)) {
            result.add(ZonedDateTime.of(startLocalDate, LocalTime.MIDNIGHT, start.getZone()));
            startLocalDate = startLocalDate.plusDays(1);
        }
        result.add(ZonedDateTime.of(startLocalDate, LocalTime.MIDNIGHT, start.getZone()));

        return result;
    }
}

In the following example Java program, we show how to use the DateUtil class above to get list of all days between two ZonedDateTime values from 1 August 2022 to 10 August 2022 and print out the result.

GetAllDaysBetweenZonedDateTimeExample.java

import java.time.ZonedDateTime;
import java.util.List;

public class GetAllDaysBetweenZonedDateTimeExample {
    public static void main(String... args) {
        ZonedDateTime start = ZonedDateTime.parse("2022-08-01T08:15:30+02:00[Europe/Paris]");
        ZonedDateTime end = ZonedDateTime.parse("2022-08-10T19:20:30+02:00[Europe/Paris]");

        // Get List of All Days Between Two ZonedDateTime objects
        List<ZonedDateTime> allDaysBetween = DateUtil.getAllDaysBetween(start, end);

        allDaysBetween.forEach(System.out::println);
    }
}
The output as below.
2022-08-01T00:00+02:00[Europe/Paris]
2022-08-02T00:00+02:00[Europe/Paris]
2022-08-03T00:00+02:00[Europe/Paris]
2022-08-04T00:00+02:00[Europe/Paris]
2022-08-05T00:00+02:00[Europe/Paris]
2022-08-06T00:00+02:00[Europe/Paris]
2022-08-07T00:00+02:00[Europe/Paris]
2022-08-08T00:00+02:00[Europe/Paris]
2022-08-09T00:00+02:00[Europe/Paris]
2022-08-10T00:00+02:00[Europe/Paris]

Happy Coding 😊

Java Get List of All Date Between Two Date

Java Get List of All Days Between Two Calendar

Java Get List of All LocalDate Between Two LocalDate

Java Get List of All Days Between Two LocalDateTime

Java Get List of All Days Between Two OffsetDateTime