Java Get List of All LocalDate Between Two LocalDate

Tags: Java LocalDate

In this Java tutorial, we learn how to write Java program to get all days between two LocalDate objects, and return as a List of LocalDate objects in Java programming language.

How to get all days between two LocalDate in Java

In this first step, we create a new Java class named DateUtil, and implement a static method named getAllLocalDateBetween(LocalDate start, LocalDate end) to return a list of LocalDate objects between a start and an end LocalDate as Java code below.

DateUtil.java

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

public class DateUtil {

    /**
     * Get List of LocalDate of all days between two LocalDate objects
     * @param start  the start LocalDate object
     * @param end the end LocalDate object
     * @return the List of all LocalDate between start and end LocalDate objects
     */
    public static List<LocalDate> getAllLocalDateBetween(LocalDate start, LocalDate end) {
        List<LocalDate> result = new ArrayList<>();
        while(start.isBefore(end)) {
            result.add(start);
            start = start.plusDays(1);
        }
        result.add(end);

        return result;
    }
}

In the following example Java program, we show how to use the DateUtil class above to get a list of days between 1 August 2022 to 5 August 2022.

GetAllLocalDateBetweenLocalDateExample.java

import java.time.LocalDate;
import java.util.List;

public class GetAllLocalDateBetweenLocalDateExample {
    public static void main(String... args) {
        LocalDate startLocalDate = LocalDate.of(2022, 8, 1);
        LocalDate endLocalDate = LocalDate.of(2022, 8, 5);

        // Get List of All LocalDate Between Two LocalDate objects
        List<LocalDate> localDates = DateUtil.getAllLocalDateBetween(startLocalDate, endLocalDate);

        localDates.forEach(System.out::println);
    }
}
The output as below.
2022-08-01
2022-08-02
2022-08-03
2022-08-04
2022-08-05

Happy Coding 😊

Java Get List of All Date Between Two Date

Java Get List of All Days Between Two LocalDateTime

Java Get List of All Days Between Two OffsetDateTime

Java Get List of All Days Between Two ZonedDateTime

Java Get List of All Days Between Two Calendar

Java Convert Date to Calendar Without Time