java 8 - how to calculate difference between two dates or java.time.LocalDate instances
This quick code reference tip with explanation first shows how to calculate the difference between two dates represented by two
java.time.LocalDate
instances using java.time.Period
class. It then shows how to get the interval between the two LocalDate
instances in hours, days and years using java.time.temporal.ChronoUnit
class.
Java 8 code to find difference between LocalDate instances using java.time.Period
[su_box title="Code to find difference between LocalDate instances using java.time.Period" style="soft" box_color="#fcba43" title_color="#00000" radius="4" Class="for-shortcodebox"][java]package com.javabrahman.java8.time;
import java.time.LocalDate;
import java.time.Month;
import java.time.Period;
public class LocalDateIntervalPeriod {
public static void main(String args[]){
LocalDate dateFrom = LocalDate.of(2015, Month.JULY, 12);
LocalDate dateTo = LocalDate.of(2016, Month.AUGUST, 22);
Period intervalPeriod = Period.between(dateFrom, dateTo);
System.out.println("Difference of days: " + intervalPeriod.getDays());
System.out.println("Difference of months: " + intervalPeriod.getMonths());
System.out.println("Difference of years: " + intervalPeriod.getYears());
}
}[/java][/su_box]
OUTPUT of the above code[su_note note_color="#1a1a1a" text_color="#DAD9D7"]Difference of days: 10
Difference of months: 1
Difference of years: 1[/su_note]
Quick explanation of the above code
java.time.Period
is a newly introduced class in Java 8.Period
holds a 'duration' or 'quantity' of time in Years, months and days.- An interval or difference of time between two given dates is a duration of time between the two dates.
- To get the duration between 2
LocalDate
instances we will use thePeriod.between()
method, which has the signature -static Period between(LocalDate startDateInclusive, LocalDate endDateExclusive)
- The duration of time between two
LocalDate
instances passed to the method is returned as aPeriod
instance itself by themethod. - The obtained duration's specific number of days, years and months breakdown can be obtained using
getDays()
,getMonths()
andgetYears()
instance method ofPeriod
. - In the above example, the difference or interval between 12-Jul-2015 and 22-Aug-2016 is obtained via the
Period.between()
method which is assigned tointervalPeriod
object of typePeriod
. - The obtained
intervalPeriod
's days, months and years of difference is then printed which is equal to 1 year, 1 month and 10 days.
java.time.temporal.ChronoUnit
is anEnum
. It provides different enum constants for different time units such asChronoUnit.DAYS
,ChronoUnit.MONTHS
,ChronoUnit.YEARS
and so on.ChronoUnit
implements thejava.time.temporal.TemporalUnit
interface.TemporalUnit
has a methodbetween()
which has the following signature -long between(Temporal temporal1Inclusive, Temporal temporal2Exclusive)
- As
LocalDate
implementsTemporal
, using the abovebetween()
method fromTemporalUnit
we can find the difference between twoLocalDate
instances in the desired unit. - To invoke the correct
between()
method implementation ofTemporal
we will access it via the requiredChronoUnit
enum constant. For example - For getting the number of days between two dates we will invokeChronoUnit.DAYS.between()
withfromDate
andtoDate
variables passed to it. - Similarly, the
ChronoUnit.MONTHS.between()
andChronoUnit.YEARS.between()
will be used to get the difference between dates in MONTHS and YEARS. - So, the difference or interval between 12-Jul-2015 and 22-Aug-2016 equals 407 days OR 13 months OR 1 year.
Tutorials on Java 8’s new Date and Time API
Overview of Java 8's new Date and Time APIClick to Read Overview of Java 8's new Date-Time API Working with time zones in Java 8| ZonedDateTime, ZoneId tutorial with examplesClick to Read tutorial on time zone handling in Java 8 How to convert LocalDate to String and String to LocalDateClick to Read tutorial on String to LocalDate conversions How to convert java.util.Date to java.time.LocalDateClick to Read tutorial on java.util.Date to LocalDate conversion Formatting localized dates in Spanish and FrenchClick to Read date formatting in Spanish & French How to get day-of-week for a given dateHow to get day-of-week using java.time.DayOfWeek enumDate Modification using TemporalAdjuster Click to Read Tutorial on TemporalAdjusters
[/su_note]