Find Difference Between Two LocalDate in Java

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:

Here is an example:

Output

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:

Here is an example:

Output

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:

Here is an example:

Output

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 call getDays() method on period object, it returns c days part only.

Here is an example:

Output

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.

Output

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.

Output

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.

Output

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.

Output

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.

Output

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.

Output

That’s all about how to find difference two LocalDate in Java.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *