Java 8 – How to get all dates between two Dates ?

In this article, we will learn how to get all Dates between start Date and end Date using Java 7, Java 8 and Java 9 versions

Get All dates between two Dates :

  1. Using Java 1.7 version – while loop
  2. Using Java 1.8 version – ChronoUnit and Stream.iterate() method
  3. Using Java 1.9 version – dateUntil() method

1. Using Java 1.7 version – while loop :

  • Steps to find all Dates between start Date & end Date
    • After formatting/parsing String to Date, convert start date to Calendar using setTime() method
    • Similarly, do/repeat above step for end Date as well
    • Now, iterate Calendar begin until Calendar end inside while-loop and get Date using getTime() method & add to ArrayList and also increment Date by 1
  • Note: start date is inclusive and end date is exclusive
  • Finally, print list of all dates to the console

GetAllDatesBetweenTwoDates.java

package in.bench.resources.java8.period.duration.examples;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class GetAllDatesBetweenTwoDates {

	public static void main(String[] args) throws ParseException {

		// create ArrayList to store dates
		List<Date> dates = new ArrayList<Date>();


		// Instantiating SimpleDateFormat class
		DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); 


		// 1. Course start date -> Calendar
		String strDate1 = "12-09-2022";
		Date startDate = formatter.parse(strDate1); // today's date
		Calendar calendarBegin = Calendar.getInstance();
		calendarBegin.setTime(startDate); // convert Date to Calendar
		System.out.println("Course Start date/calendar is :- \n" + startDate);


		// 2. Course end date -> Calendar
		String strDate2 = "23-09-2022";
		Date endDate = formatter.parse(strDate2); // future date
		Calendar calendarEnd = Calendar.getInstance();
		calendarEnd.setTime(endDate); // convert Date to Calendar
		System.out.println("\nCourse End date/calendar is :- \n" + endDate + "\n");


		// 3. find all Dates between calendarBegin & calendarEnd
		while (calendarBegin.before(calendarEnd)) {

			// get Date & add to List and increment Date by 1 day
			Date date = calendarBegin.getTime();
			dates.add(date);
			calendarBegin.add(Calendar.DATE, 1);
		}


		// print result to console
		System.out.println("Dates between (" + startDate + ") & (" + endDate + ") is :- ");
		for(Date date :  dates) {
			System.out.println(date);
		}
	}
}

Output:

Course Start date/calendar is :- 
Mon Sep 12 00:00:00 IST 2022

Course End date/calendar is :- 
Fri Sep 23 00:00:00 IST 2022

Dates between (Mon Sep 12 00:00:00 IST 2022) & (Fri Sep 23 00:00:00 IST 2022) is :- 
Mon Sep 12 00:00:00 IST 2022
Tue Sep 13 00:00:00 IST 2022
Wed Sep 14 00:00:00 IST 2022
Thu Sep 15 00:00:00 IST 2022
Fri Sep 16 00:00:00 IST 2022
Sat Sep 17 00:00:00 IST 2022
Sun Sep 18 00:00:00 IST 2022
Mon Sep 19 00:00:00 IST 2022
Tue Sep 20 00:00:00 IST 2022
Wed Sep 21 00:00:00 IST 2022
Thu Sep 22 00:00:00 IST 2022

2. Using Java 1.8 version – ChronoUnit & Stream.iterate() method :

  • Steps to find all Dates between 2 LocalDate in Java 1.8 version –
    • First, calculate number of days between start LocalDate & end LocalDate using ChronoUnit.DAYS.between() method as this will help to put an upper limit while iterating using Stream.iterate() method
    • Now, to get all dates iterate LocalDate starting from start LocalDate and thus incrementing Date by 1 using plusDays() method putting an upperlimit obtained from previous step and collecting to a List using collect() method
  • Note: start date is inclusive and end date is exclusive
  • Finally, print list of all dates to the console

GetAllDatesBetweenTwoLocalDate1.java

package in.bench.resources.java8.period.duration.examples;

import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class GetAllDatesBetweenTwoLocalDate1 {

	public static void main(String[] args) {

		// 1. Course start date
		LocalDate localDateStart = LocalDate.of(2022, Month.SEPTEMBER, 12); // today's date
		System.out.println("Course Start date is :- \n" + localDateStart);


		// 2. Course end date -> Calendar
		LocalDate localDateEnd = LocalDate.of(2022, Month.SEPTEMBER, 23); // future date
		System.out.println("\nCourse End date is :- \n" + localDateEnd);


		// 3. get number of Days between start/end LocalDate
		long days = ChronoUnit.DAYS.between(localDateStart, localDateEnd);
		System.out.println("\nNumber of Days between (" + localDateStart 
				+ ") & (" +  localDateEnd + ") is :- \n" + days);


		// 4. get all Dates in List
		List<LocalDate> dateList = Stream
				.iterate(localDateStart, localDate -> localDate.plusDays(1))
				.limit(days)
				.collect(Collectors.toList());


		// 5. print all dates to console
		System.out.println("\nDates between (" + localDateStart + ") & (" + localDateEnd + ") is :- ");
		dateList.forEach(System.out::println);
	}
}

Output:

Course Start date is :- 
2022-09-12

Course End date is :- 
2022-09-23

Number of Days between (2022-09-12) & (2022-09-23) is :- 
11

Dates between (2022-09-12) & (2022-09-23) is :- 
2022-09-12
2022-09-13
2022-09-14
2022-09-15
2022-09-16
2022-09-17
2022-09-18
2022-09-19
2022-09-20
2022-09-21
2022-09-22

3. Using Java 1.9 version – dateUntil() method :

  • Steps to find all Dates between 2 LocalDate in Java 1.9 version –
    • It is very straightforward as in Java 1.9 version a new method called dateUntil() introduced in LocalDate class which returns List of Dates between 2 LocalDate
  • Note: start date is inclusive and end date is exclusive
  • Finally, print list of all dates to the console

GetAllDatesBetweenTwoLocalDate2.java

package in.bench.resources.java9.period.duration.examples;

import java.time.LocalDate;
import java.time.Month;
import java.util.List;
import java.util.stream.Collectors;

public class GetAllDatesBetweenTwoLocalDate2 {

	public static void main(String[] args) {

		// 1. Course start date
		LocalDate localDateStart = LocalDate.of(2022, Month.SEPTEMBER, 12); // today's date
		System.out.println("Course Start date is :- \n" + localDateStart);


		// 2. Course end date -> Calendar
		LocalDate localDateEnd = LocalDate.of(2022, Month.SEPTEMBER, 23); // future date
		System.out.println("\nCourse End date is :- \n" + localDateEnd);


		// 3. get all Dates in List
		List<LocalDate> dateList = localDateStart.datesUntil(localDateEnd)
				.collect(Collectors.toList());


		// 4. print all dates to console
		System.out.println("\nDates between (" + localDateStart + ") & (" + localDateEnd + ") is :- ");
		dateList.forEach(System.out::println);
	}
}

Output:

Course Start date is :- 
2022-09-12

Course End date is :- 
2022-09-23

Dates between (2022-09-12) & (2022-09-23) is :- 
2022-09-12
2022-09-13
2022-09-14
2022-09-15
2022-09-16
2022-09-17
2022-09-18
2022-09-19
2022-09-20
2022-09-21
2022-09-22

Related Articles :

References :

Happy Coding !!
Happy Learning !!

Java 8 – How to get current Date in different ways ?
Java 8 – What are the different ways to calculate difference between 2 Date/time ?