Table of Contents
- Ways to find difference between two LocalDate in Java
- Frequently asked questions
- How to find difference between two LocalDate in Java in Days?
- How to find difference between two LocalDate in Java in Years?
- How to find difference between two LocalDate in Java in Months?
- How to find difference between two LocalDate in Java in Hours?
- How to find difference between two LocalDate in Java in Minutes?
- How to find difference between two LocalDate in Java in seconds?
In this post, we will see how to find difference between two LocalDate in Java.
Ways to find difference between two LocalDate in Java
There are multiple ways to find difference between two LocalDate in various time units such as Days, Months, Years etc.
Using LocalDate’s until() method
We can use LocalDate’s until()
method to find difference between two LocalDate.
LocalDate’s until()
method returns amount of time with another date in terms of given unit. You can pass specific TemporalUnits such as MONTHS, DAYS to get result in specified units.
For example:
If you want amount of days between two LocalDate, we can use following syntax:
1 2 3 |
startDate.until(endDate, DAYS) |
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package org.arpit.java2blog; import java.time.LocalDate; import java.time.Month; import java.time.temporal.ChronoUnit; public class DifferenceBetweenTwoLocalDateUntil { public static void main(String[] args) { LocalDate dateBefore = LocalDate.of(2022, Month.AUGUST, 23); LocalDate dateAfter = LocalDate.of(2022, Month.DECEMBER, 25); long noOfYears = dateBefore.until(dateAfter, ChronoUnit.YEARS); long noOfMonths = dateBefore.until(dateAfter, ChronoUnit.MONTHS); long noOfWeeks = dateBefore.until(dateAfter, ChronoUnit.WEEKS); long noOfDays = dateBefore.until(dateAfter, ChronoUnit.DAYS); System.out.println("Years: "+noOfYears); System.out.println("Months: "+noOfMonths); System.out.println("Weeks: "+noOfWeeks); System.out.println("Days: "+noOfDays); } } |
Output
1 2 3 4 5 6 |
Years: 0 Months: 4 Weeks: 17 Days: 124 |
Using ChronoUnit Enum
We can use ChronoUnit
method to find difference between two LocalDate.
It intenally calls until()
method to find the difference between to time unit.
For example:
If you want amount of days between two LocalDate, we can use following syntax:
1 2 3 |
ChronoUnit.YEARS.between(dateBefore, dateAfter); |
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package org.arpit.java2blog import java.time.LocalDate; import java.time.Month; import java.time.temporal.ChronoUnit; public class DifferenceBetweenTwoLocalDateChronoUnit { public static void main(String[] args) { LocalDate dateBefore = LocalDate.of(2022, Month.AUGUST, 23); LocalDate dateAfter = LocalDate.of(2022, Month.DECEMBER, 25); long noOfYears = ChronoUnit.YEARS.between(dateBefore, dateAfter); long noOfMonths = ChronoUnit.MONTHS.between(dateBefore, dateAfter); long noOfWeeks = ChronoUnit.WEEKS.between(dateBefore, dateAfter); long noOfDays = ChronoUnit.DAYS.between(dateBefore, dateAfter); System.out.println("Years: "+noOfYears); System.out.println("Months: "+noOfMonths); System.out.println("Weeks: "+noOfWeeks); System.out.println("Days: "+noOfDays); } } |
Output
1 2 3 4 5 6 |
Years: 0 Months: 4 Weeks: 17 Days: 124 |
Using Duration’s between() method
We can use Duration’s between()
method to find difference between two LocalDate.
Duration’s between()
method returns amount of time with another date in terms of minutes, hours, seconds and nanoseconds. You can use toXXX()
method to convert into required unit.
For example:
If you want amount of days between two LocalDate, we can use following syntax:
1 2 3 |
Duration.between(dateBefore.atStartOfDay(), dateAfter.atStartOfDay()).toHours(); |
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package org.arpit.java2blog; import java.time.Duration; import java.time.LocalDate; import java.time.Month; public class DifferenceBetweenTwoLocalDateDuration { public static void main(String[] args) { LocalDate dateBefore = LocalDate.of(2022, Month.AUGUST, 23); LocalDate dateAfter = LocalDate.of(2022, Month.DECEMBER, 25); long noOfDays = Duration.between(dateBefore.atStartOfDay(), dateAfter.atStartOfDay()).toDays(); long noOfHours = Duration.between(dateBefore.atStartOfDay(), dateAfter.atStartOfDay()).toHours(); long noOfMinutes = Duration.between(dateBefore.atStartOfDay(), dateAfter.atStartOfDay()).toMinutes(); long noOfSeconds = Duration.between(dateBefore.atStartOfDay(), dateAfter.atStartOfDay()).toSeconds(); System.out.println("Days: "+noOfDays); System.out.println("Hours: "+noOfHours); System.out.println("Minutes: "+noOfMinutes); System.out.println("Seconds: "+noOfSeconds); } } |
Output
1 2 3 4 5 6 |
Days: 124 Hours: 2976 Minutes: 178560 Seconds: 10713600 |
Using Period’s between() method
We can use Period’s between()
method to find difference between two LocalDate.
Period’s between()
method date based amount of time in the ISO-8601 calendar system, such as 3 years, 8 months and 5 days
. This class provide output in terms of years, months and days.
- Start date is included, but end date is excluded
- It can return negative result if end date is before start date.
As Period class represents time in terms of format
a years, b months and c days
, so when you callgetDays()
method on period object, it returnsc days
part only.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package org.arpit.java2blog; import java.time.LocalDate; import java.time.Month; import java.time.Period; public class DifferenceBetweenTwoLocalDatePeriod { public static void main(String[] args) { LocalDate localDate1 = LocalDate.of(1999, Month.AUGUST, 23); LocalDate localDate2 = LocalDate.of(2022, Month.DECEMBER, 25); Period period = Period.between(localDate1, localDate2); System.out.print(period.getYears() + " years,"); System.out.print(period.getMonths() + " months,"); System.out.print(period.getDays() + " days"); } } |
Output
1 2 3 |
23 years,4 months,2 days |
Further reading:
Frequently asked questions
How to find difference between two LocalDate in Java in Days?
You can use DAYS
‘s between method to get difference between two LocalDate in Java. It is exact same method as ChronoUnit
enum, only difference is to use import static to directly import the java.time.temporal.ChronoUnit.DAYS
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; import java.time.LocalDate; import java.time.Month; import static java.time.temporal.ChronoUnit.DAYS; public class DifferenceBetweenTwoLocalDateDays { public static void main(String[] args) { LocalDate beforeDate = LocalDate.of(2022, Month.AUGUST, 23); LocalDate afterDate = LocalDate.of(2022, Month.DECEMBER, 25); long noOfDays = DAYS.between(beforeDate, afterDate); System.out.println("Days: "+noOfDays); } } |
Output
1 2 3 |
Days: 124 |
How to find difference between two LocalDate in Java in Years?
You can use YEARS
‘s between method to get difference between two LocalDate in Java. It is exact same method as ChronoUnit
enum, only difference is to use import static to directly import the java.time.temporal.ChronoUnit.YEARS
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package org.arpit.java2blog; import java.time.LocalDate; import java.time.Month; import static java.time.temporal.ChronoUnit.YEARS; public class DifferenceBetweenTwoLocalDateDays { public static void main(String[] args) { LocalDate beforeDate = LocalDate.of(1999, Month.AUGUST, 23); LocalDate afterDate = LocalDate.of(2022, Month.DECEMBER, 25); long noOfYears = YEARS.between(beforeDate, afterDate); System.out.println("YEARS: "+noOfYears); } } |
Output
1 2 3 |
YEARS: 23 |
How to find difference between two LocalDate in Java in Months?
You can use MONTHS
‘s between method to get difference between two LocalDate in Java. It is exact same method as ChronoUnit
enum, only difference is to use import static to directly import the java.time.temporal.ChronoUnit.MONTHS
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; import java.time.LocalDate; import java.time.Month; import static java.time.temporal.ChronoUnit.MONTHS; public class DifferenceBetweenTwoLocalDateMonths { public static void main(String[] args) { LocalDate beforeDate = LocalDate.of(2022, Month.AUGUST, 23); LocalDate afterDate = LocalDate.of(2022, Month.DECEMBER, 25); long noOfMonths = MONTHS.between(beforeDate, afterDate); System.out.println("Months: "+noOfMonths); } } |
Output
1 2 3 |
Months: 4 |
How to find difference between two LocalDate in Java in Hours?
You can use Duration
‘s between method to get difference between two LocalDate in Java. You need to call LocalDate
‘s atStartOfDay()
method to convert LocalDate to LocalDateTime and use it Duration
‘s between()
method. Call toHours()
to get desired results in hours.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; import java.time.Duration; import java.time.LocalDate; import java.time.Month; public class DifferenceBetweenTwoLocalDateHours { public static void main(String[] args) { LocalDate beforeDate = LocalDate.of(2022, Month.AUGUST, 23); LocalDate afterDate = LocalDate.of(2022, Month.DECEMBER, 25); long noOfHours = Duration.between(beforeDate.atStartOfDay(), afterDate.atStartOfDay()).toHours(); System.out.println("Hours: "+noOfHours); } } |
Output
1 2 3 |
Hours: 2976 |
How to find difference between two LocalDate in Java in Minutes?
You can use Duration
‘s between method to get difference between two LocalDate in Java. You need to call LocalDate
‘s atStartOfDay()
method to convert LocalDate to LocalDateTime and use it Duration
‘s between()
method. Call toMinutes()
to get desired results in minutes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; import java.time.Duration; import java.time.LocalDate; import java.time.Month; public class DifferenceBetweenTwoLocalDateDays { public static void main(String[] args) { LocalDate beforeDate = LocalDate.of(2022, Month.AUGUST, 23); LocalDate afterDate = LocalDate.of(2022, Month.DECEMBER, 25); long noOfMinutes = Duration.between(beforeDate.atStartOfDay(), afterDate.atStartOfDay()).toMinutes(); System.out.println("Minutes: "+noOfMinutes); } } |
Output
1 2 3 |
Minutes: 178560 |
How to find difference between two LocalDate in Java in seconds?
You can use Duration
‘s between method to get difference between two LocalDate in Java. You need to call LocalDate
‘s atStartOfDay()
method to convert LocalDate to LocalDateTime and use it Duration
‘s between()
method. Call toMinutes()
to get desired results in minutes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; import java.time.Duration; import java.time.LocalDate; import java.time.Month; public class DifferenceBetweenTwoLocalDateDays { public static void main(String[] args) { LocalDate beforeDate = LocalDate.of(2022, Month.AUGUST, 23); LocalDate afterDate = LocalDate.of(2022, Month.DECEMBER, 25); long noofSeconds = Duration.between(beforeDate.atStartOfDay(), afterDate.atStartOfDay()).toSeconds(); System.out.println("Seconds: "+noofSeconds); } } |
Output
1 2 3 |
Seconds: 10713600 |
That’s all about how to find difference two LocalDate in Java.