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
- Pass organization joining date as 1st argument
- Pass organization relieving date as 2nd argument
- Period class has many useful method like,
- getYears() – returns numbers of years between 2 LocalDate instances
- getMonths() – returns numbers of months between 2 LocalDate instances
- 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
- getTime() method of Date returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this
- 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:
- Java 8 – LocalDate with method details and examples
- Java 8 – How to get Year, Month and Day fields from LocalDate ?
- Java 8 – How to get Number of Days in a Month from LocalDate ?
- Java 8 – How to get Number of Days in a Year from LocalDate ?
- Java 8 – How to form LocalDate passing Year, Month and Day fields ?
- Java 8 – How to parse LocalDate in String form ?
- Java 8 – How to convert String to LocalDate ?
- Java 8 – How to convert LocalDate to String ?
- Java 8 – How to convert LocalDate in different formats ?
- Java 8 – How to convert LocalDate in different Format Style ?
- Java 8 – How to convert LocalDate to LocalDateTime ?
- Java 8 – How to convert LocalDate to ZonedDateTime ?
- Java 8 – How to convert LocalDate to an OffsetDateTime ?
- Java 8 – How to convert LocalDate to an Instant ?
- Java 8 – How to convert LocalDate to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Calendar and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDate to javax.xml.datatype.XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDate in different ways ?
- Java 8 – How to add Year, Month and Day fields to LocalDate ?
- Java 8 – How to subtract Year, Month and Day fields from LocalDate ?
- Java 8 – How to alter Year, Month and Day fields of LocalDate ?
- Java 8 – How to check whether a LocalDate is Before another LocalDate ?
- Java 8 – How to check whether a LocalDate is After another LocalDate ?
- Java 8 – How to compare two LocalDate instances ?
- Java 8 – How to get remaining number of Days in a Year using LocalDate ?
- Java 8 – How to find difference between two LocalDate instances using Period ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/8/docs/api/java/time/Period.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
- https://docs.oracle.com/javase/8/docs/api/java/util/GregorianCalendar.html
Happy Coding !!
Happy Learning !!