Java 8 – How to find difference between two LocalDate instances using Period ?

In this article, we will learn how to find difference between two LocalDate and Calendar/Date instances

1. Find difference between two LocalDate instances :

  • To find difference between two LocalDate instances, we can use Period.between() method
  • Period.between accepts 2 input-arguments as LocalDate and returns difference of two LocalDate as Period
    1. Pass organization joining date as 1st argument
    2. Pass organization relieving date as 2nd argument
  • Period class has many useful method like,
    1. getYears() – returns numbers of years between 2 LocalDate instances
    2. getMonths() – returns numbers of months between 2 LocalDate instances
    3. getDays() – returns numbers of days between 2 LocalDate instances
  • Finally print Period spent as Years, Months and Days in an organization to the console

FindDifferenceOfTwoLocalDate.java

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

import java.time.LocalDate;
import java.time.Period;

public class FindDifferenceOfTwoLocalDate {

	public static void main(String[] args) {

		// 1. organization joining date
		LocalDate localDate1 = LocalDate.of(2017, 3, 6);
		System.out.println("Organization Joining Date is :- \n" + localDate1);


		// 2. organization relieving date
		LocalDate localDate2 = LocalDate.of(2019, 6, 28);
		System.out.println("\nOrganization Relieving Date is :- \n" + localDate2);


		// 3. difference between 2 LocalDate - Days spent in a organization
		Period period = Period.between(localDate1, localDate2);
		System.out.println("\nPeriod between 2 LocalDate is :- \n" + period);


		// 3.1 get difference of years/months/days
		int years = period.getYears();
		int months = period.getMonths();
		int days = period.getDays();


		// 3.2 print to console
		System.out.println("\nPeriod spent in an Organization is :- \n"
				+ years + " Years " 
				+ months + " Months " 
				+ days + " Days");

		// 3.3 number of months spent in an organization
		System.out.print("\nNumber of Months spent in an organization :- \n" 
				+ period.toTotalMonths());
	}
}

Output:

Organization Joining Date is :- 
2017-03-06

Organization Relieving Date is :- 
2019-06-28

Period between 2 LocalDate is :- 
P2Y3M22D

Period spent in an Organization is :- 
2 Years 3 Months 22 Days

Number of Months spent in an organization :- 
27

2. Find difference between two Calendar/Date instances :

  • First, get organization joining Date by instantiating GregorianCalendar and passing Year, Month and Day fields/values to the constructor
  • Similarly, get 2nd Date as organization relieving Date by instantiating GregorianCalendar and passing Year, Month and Day fields/values to the constructor
    • getTime() method of Date returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object
  • Subtract joining date from relieving date which returns difference between 2 Dates in terms of milliseconds
    • Using this difference in Milliseconds, calculate days and years as shown in the below illustration
  • Finally print Years/Days spent in an organization to the console

FindDifferenceOfTwoCalendarDate.java

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

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class FindDifferenceOfTwoCalendarDate {

	public static void main(String[] args) {

		// 1. organization joining date - Instantiate GregorianCalendar
		Calendar calendar1 = new GregorianCalendar(2017, 2, 6); // March
		Date date1 = calendar1.getTime();
		System.out.println("Organization Joining Date is :- \n" + date1);


		// 2. organization relieving date - Instantiate GregorianCalendar
		Calendar calendar2 = new GregorianCalendar(2019, 5, 28); // June
		Date date2 = calendar2.getTime();
		System.out.println("\nOrganization Relieving Date is :- \n" + date2);


		// 3. difference between 2 Date - Days spent in a organization
		long differenceInMillis = date2.getTime() - date1.getTime();


		// 3.1 get difference of years/days
		long years = (differenceInMillis / (365 * 24 * 60 * 60 * 1000l)); 
		long days = (differenceInMillis / (24 * 60 * 60 * 1000l)) % 365;


		// 3.2 print to console
		System.out.print("\nDays spent in an Organization is :- \n"
				+ years + " Years " 
				+ days + " Days");
	}
}

Output:

Organization Joining Date is :- 
Mon Mar 06 00:00:00 IST 2017

Organization Relieving Date is :- 
Fri Jun 28 00:00:00 IST 2019

Days spent in an Organization is :- 
2 Years 114 Days

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert LocalDate to LocalDateTime ?
Java 8 – How to get remaining number of Days in a Year using LocalDate ?