Java 8 – How to calculate number of Days between two LocalDate instances ?

In this article, we will learn how to calculate number of Days between two Date instances

1. Number of Days between two LocalDate :

Both the below methods uses ChronoUnit but slightly in a different way,

  1. Using ChronoUnit.DAYS.between() method
  2. Using LocalDate.until() method

1.1 Using ChronoUnit.DAYS.between() method :

  • ChronoUnit.DAYS.between() method accepts 2 input-arguments of LocalDate instances which calculates difference between them in terms of number of Days
  • Note: if 1st argument Date is greater-than 2nd argument Date then result will be negative

CalculateDaysBetweenTwoDates1.java

package in.bench.resources.java8.localdate.examples;

import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoUnit;

public class CalculateDaysBetweenTwoDates1 {

	public static void main(String[] args) {

		// 1. semester start date
		LocalDate startLocalDate = LocalDate.of(2022, Month.SEPTEMBER, 8); // today
		System.out.println("Semester start date is :- \n" + startLocalDate);


		// 2. semester end date
		LocalDate endLocalDate = LocalDate.of(2022, Month.DECEMBER, 22);
		System.out.println("\nSemester end date is :- \n" + endLocalDate);


		// find difference in DAYS
		long diffInDays = ChronoUnit.DAYS.between(startLocalDate, endLocalDate);
		System.out.print("\nNumber of Days in above Semester is :- \n" + diffInDays);
	}
}

Output:

Semester start date is :- 
2022-09-08

Semester end date is :- 
2022-12-22

Number of Days in above Semester is :- 
105

1.2 Using LocalDate.until() method :

  • LocalDate.until() method accepts 2 input-arguments
    • 1st argument is the Date to which invoking Date needs to be compared/calculated
    • 2nd argument is the ChronoUnit in which result/difference is required like DAYS, MONTHS, HOURS, etc.,
  • Note: if invoking Date is greater-than passed/supplied Date then result will be negative

CalculateDaysBetweenTwoDates2.java

package in.bench.resources.java8.localdate.examples;

import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoUnit;

public class CalculateDaysBetweenTwoDates2 {

	public static void main(String[] args) {

		// 1. semester start date
		LocalDate localDate1 = LocalDate.of(2023, Month.JANUARY, 17); // today
		System.out.println("Semester start date is :- \n" + localDate1);


		// 2. semester end date
		LocalDate localDate2 = LocalDate.of(2023, Month.JUNE, 5);
		System.out.println("\nSemester end date is :- \n" + localDate2);


		// find difference in DAYS
		long diffInDays = localDate1.until(localDate2, ChronoUnit.DAYS);
		System.out.print("\nNumber of Days in above Semester is :- \n" + diffInDays);
	}
}

Output:

Semester start date is :- 
2023-01-17

Semester end date is :- 
2023-06-05

Number of Days in above Semester is :- 
139

2. Number of Days between two Dates :

  1. Using toInstant() and ChronoUnit.DAYS.between() methods
  2. Using TimeUnit
  3. Using getTime() method of Date class
  4. Using parse() and format() methods

2.1 Using toInstant() and ChronoUnit.DAYS.between() methods :

  • First, parse the given String to Date using DateFormat/SimpleDateFormat classes
  • To find the difference between two Dates, use ChronoUnit.DAYS.between() method after converting Date to Instant using toInstant() method
  • ChronoUnit.DAYS.between() method accepts 2 input-arguments of Instant which calculates difference between them in terms of number of Days
  • Note: if 1st argument Instant is greater-than 2nd argument Instant then result will be negative

CalculateDaysBetweenTwoDates3.java

package in.bench.resources.java8.localdate.examples;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.temporal.ChronoUnit;
import java.util.Date;

public class CalculateDaysBetweenTwoDates3 {

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

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


		// 1. semester start date
		String strDate1 = "08-09-2022";
		Date date1 = formatter.parse(strDate1); // today's date
		System.out.println("Semester start date is :- \n" + date1);


		// 2. semester end date
		String strDate2 = "23-12-2022";
		Date date2 = formatter.parse(strDate2); // future date
		System.out.println("\nSemester start date is :- \n" + date2);


		// find difference in DAYS
		long diffInDays = ChronoUnit.DAYS.between(date1.toInstant(), date2.toInstant());
		System.out.print("\nNumber of Days in above Semester is :- \n" + diffInDays);
	}
}

Output:

Semester start date is :- 
Thu Sep 08 00:00:00 IST 2022

Semester start date is :- 
Fri Dec 23 00:00:00 IST 2022

Number of Days in above Semester is :- 
106

2.2 Using TimeUnit :

  • First, parse the given String to Date using DateFormat/SimpleDateFormat classes
  • To find the difference between two Dates, convert Date into milliseconds using getTime() method and calculate their difference in terms of milliseconds
  • For conversion from Milliseconds to Days, use TimeUnit.DAYS.convert() method which accepts 2 input-arguments,
    • 1st argument is the difference of two Date instances
    • 2nd argument is the TimeUnit in which difference is passed

CalculateDaysBetweenTwoDates4.java

package in.bench.resources.java8.localdate.examples;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class CalculateDaysBetweenTwoDates4 {

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

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


		// 1. semester start date
		String strDate1 = "08-09-2022";
		Date date1 = formatter.parse(strDate1); // today's date
		System.out.println("Semester start date is :- \n" + date1);


		// 2. semester end date
		String strDate2 = "23-12-2022";
		Date date2 = formatter.parse(strDate2); // future date
		System.out.println("\nSemester start date is :- \n" + date2);


		// find difference in DAYS
		long diff = date2.getTime() - date1.getTime();
		long diffInDays = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
		System.out.print("\nNumber of Days in above Semester is :- \n" + diffInDays);
	}
}

Output:

Semester start date is :- 
Thu Sep 08 00:00:00 IST 2022

Semester start date is :- 
Fri Dec 23 00:00:00 IST 2022

Number of Days in above Semester is :- 
106

2.3 Using getTime() method of Date class :

  • First, parse the given String to Date using DateFormat/SimpleDateFormat classes
  • To find the difference between two Dates, convert Date into milliseconds using getTime() method and calculate their difference in terms of milliseconds
  • For conversion from Milliseconds to Days, divide the difference in milliseconds by (1000 * 60 * 60 * 24 = 8,64,00,000) which will return absolute/rounded-off value

CalculateDaysBetweenTwoDates5.java

package in.bench.resources.java8.localdate.examples;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CalculateDaysBetweenTwoDates5 {

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

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


		// 1. semester start date
		String strDate1 = "08-09-2022";
		Date date1 = formatter.parse(strDate1); // today's date
		System.out.println("Semester start date is :- \n" + date1);


		// 2. semester end date
		String strDate2 = "23-12-2022";
		Date date2 = formatter.parse(strDate2); // future date
		System.out.println("\nSemester start date is :- \n" + date2);


		// find difference in DAYS
		long diff = date2.getTime() - date1.getTime();
		float days = (diff / (1000 * 60 * 60 * 24));
		System.out.print("\nNumber of Days in above Semester is :- \n" + Math.round(days));
	}
}

Output:

Semester start date is :- 
Thu Sep 08 00:00:00 IST 2022

Semester start date is :- 
Fri Dec 23 00:00:00 IST 2022

Number of Days in above Semester is :- 
106

2.4 Using parse() and format() methods :

  • First, parse the given String to Date using DateFormat/SimpleDateFormat classes
  • And then convert Date to LocalDate using LocalDate.parse() method
  • To find the difference between two Dates, use LocalDate.until() method
  • LocalDate.until() method accepts 2 input-arguments
    • 1st argument is the Date to which invoking Date needs to be compared/calculated
    • 2nd argument is the ChronoUnit in which result is required like DAYS, MONTHS, HOURS, etc.,
  • Note: if invoking Date is greater-than passed/supplied Date then result will be negative

CalculateDaysBetweenTwoDates6.java

package in.bench.resources.java8.localdate.examples;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Date;

public class CalculateDaysBetweenTwoDates6 {

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

		// 1. Parsing String to java.util.Date
		DateFormat formatter1 = new SimpleDateFormat("dd-MM-yyyy");


		// 2. Formatting java.util.Date to LocalDate
		DateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");


		// 1. semester start date
		String strDate1 = "08-09-2022";
		Date date1 = formatter1.parse(strDate1); // today's date
		LocalDate localDate1 = LocalDate.parse(formatter2.format(date1));
		System.out.println("Semester start date is :- \n" + localDate1);


		// 2. semester end date
		String strDate2 = "23-12-2022";
		Date date2 = formatter1.parse(strDate2); // future date
		LocalDate localDate2 = LocalDate.parse(formatter2.format(date2));
		System.out.println("\nSemester start date is :- \n" + localDate2);


		// find difference in DAYS
		long diffInDays = localDate1.until(localDate2, ChronoUnit.DAYS);
		System.out.print("\nNumber of Days in above Semester is :- \n" + diffInDays);}
}

Output:

Semester start date is :- 
2022-09-08

Semester start date is :- 
2022-12-23

Number of Days in above Semester is :- 
106

Related Articles :

References :

Happy Coding !!
Happy Learning !!

Java 8 – What are the different ways to calculate difference between 2 Date/time ?
Java 8 – How to convert LocalTime to java.sql.Time and vice-versa ?