Java Get List of All Days Between Two Calendar

Tags: Java Calendar DateUtil

In this Java tutorial, we learn how to write Java utility class to get all days between two Calendar values and return it as a List of Calendar objects in Java programming language.

How to get all days between two Calendar objects

Firstly, create a new Java class named DateUtil, and implement getAllDaysBetween(Calendar start, Calendar end) method which return all days between two Calendar values and return it as a List of Calendar objects as Java code below.

DateUtil.java

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class DateUtil {

    /**
     * This method to get list of all days between two Calendar objects and return it as List of Calendar
     * @param start the start Calendar object
     * @param end the end Calendar object
     * @return the list of all days between start and end
     */
    public static List<Calendar> getAllDaysBetween(Calendar start, Calendar end) {
        List<Calendar> result = new ArrayList<>();
        Calendar startCalendar = createNewCalendarWithoutTime(start);
        Calendar endCalendar = createNewCalendarWithoutTime(end);
        while(startCalendar.before(endCalendar)) {
            result.add(createNewCalendarWithoutTime(startCalendar));
            startCalendar.add(Calendar.DATE, 1);
        }
        result.add(createNewCalendarWithoutTime(startCalendar));

        return result;
    }

    /**
     * Create a new Calendar from a given Calendar and set time to zero
     * @param calendar the input Calendar object
     * @return the new Calendar object
     */
    public static Calendar createNewCalendarWithoutTime(Calendar calendar) {
        Calendar result = Calendar.getInstance();
        result.setTime(calendar.getTime());
        result.set(Calendar.MILLISECOND, 0);
        result.set(Calendar.SECOND, 0);
        result.set(Calendar.MINUTE, 0);
        result.set(Calendar.HOUR_OF_DAY, 0);
        return  result;
    }
}

In the following example Java program, we show how to use the above DateUtil class to get List of all days between two Calendar objects from 1 August 2022 to 5 August 2022 and print out the result.

GetAllDaysBetweenCalendarExample.java

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

public class GetAllDaysBetweenCalendarExample {
    public static void main(String... args) {
        Calendar start = new GregorianCalendar(2022, 8, 1);
        Calendar end = new GregorianCalendar(2022, 8, 5);

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

        allDaysBetween.forEach(calendar -> System.out.println(calendar.getTime()));
    }
}
The output as below.
Thu Sep 01 00:00:00 ICT 2022
Fri Sep 02 00:00:00 ICT 2022
Sat Sep 03 00:00:00 ICT 2022
Sun Sep 04 00:00:00 ICT 2022
Mon Sep 05 00:00:00 ICT 2022

Happy Coding 😊

Java Get List of All Date Between Two Date

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

Java Get List of All Days Between Two ZonedDateTime