Table of Contents
In this post, we will see how to find difference between two LocalDateTime in Java.
Ways to find difference between two LocalDateTime in Java
There are multiple ways to find difference between two LocalDateTime in various time units such as Days, Months, Years etc.
Using LocalDateTime’s until() method
We can use LocalDateTime’s until()
method to find difference between two LocalDateTime.
LocalDateTime’s until()
method returns amount of time with another date in terms of given unit. You can pass specific Temporal units such as MONTHS, DAYS, SECONDS to get result in specified units.
For example:
If you want amount of seconds between two LocalDateTime, we can use following syntax:
1 2 3 |
long noOfSeconds = dateBefore.until(dateAfter, ChronoUnit.SECONDS); |
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 25 26 27 28 29 30 |
package org.arpit.java2blog; import java.time.LocalDateTime; import java.time.Month; import java.time.temporal.ChronoUnit; public class DifferenceBetweenTwoLocalDateTimeUntil { public static void main(String[] args) { LocalDateTime dateBefore = LocalDateTime.of(2022, Month.AUGUST, 23,6,30,20); LocalDateTime dateAfter = LocalDateTime.of(2022, Month.DECEMBER, 25,2,56,32); 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); long noOfHours = dateBefore.until(dateAfter, ChronoUnit.HOURS); long noOfMinutes = dateBefore.until(dateAfter, ChronoUnit.MINUTES); long noOfSeconds = dateBefore.until(dateAfter, ChronoUnit.SECONDS); System.out.println("Years: "+noOfYears); System.out.println("Months: "+noOfMonths); System.out.println("Weeks: "+noOfWeeks); 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 7 8 9 |
Years: 0 Months: 4 Weeks: 17 Days: 123 Hours: 2972 Minutes: 178346 Seconds: 10700772 |
Using ChronoUnit Enum
We can use ChronoUnit
method to find difference between two LocalDateTime.
It intenally calls until()
method to find the difference between to time units.
For example:
If you want amount of seconds between two LocalDateTime, we can use following syntax:
1 2 3 |
long noOfSeconds = ChronoUnit.SECONDS.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 25 26 27 28 29 30 |
package org.arpit.java2blog.entry; import java.time.LocalDateTime; import java.time.Month; import java.time.temporal.ChronoUnit; public class DifferenceBetweenTwoLocalDateTime { public static void main(String[] args) { LocalDateTime dateBefore = LocalDateTime.of(2022, Month.AUGUST, 23,6,30,20); LocalDateTime dateAfter = LocalDateTime.of(2022, Month.DECEMBER, 25,2,56,32); 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); long noOfHours = ChronoUnit.HOURS.between(dateBefore,dateAfter); long noOfMinutes = ChronoUnit.MINUTES.between(dateBefore,dateAfter); long noOfSeconds = ChronoUnit.SECONDS.between(dateBefore,dateAfter); System.out.println("Years: "+noOfYears); System.out.println("Months: "+noOfMonths); System.out.println("Weeks: "+noOfWeeks); 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 7 8 9 |
Years: 0 Months: 4 Weeks: 17 Days: 123 Hours: 2972 Minutes: 178346 Seconds: 10700772 |
Using Duration’s between() method
We can use Duration’s between()
method to find difference between two LocalDateTime.
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 seconds between two LocalDateTime, we can use following syntax:
1 2 3 |
long noOfSeconds = Duration.between(dateBefore, dateAfter).toSeconds(); |
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.entry; import java.time.Duration; import java.time.LocalDateTime; import java.time.Month; public class DifferenceBetweenTwoLocalDateTimeDuration { public static void main(String[] args) { LocalDateTime dateBefore = LocalDateTime.of(2022, Month.AUGUST, 23,6,30,20); LocalDateTime dateAfter = LocalDateTime.of(2022, Month.DECEMBER, 25,2,56,32); long noOfDays = Duration.between(dateBefore, dateAfter).toDays(); long noOfHours = Duration.between(dateBefore, dateAfter).toHours(); long noOfMinutes = Duration.between(dateBefore, dateAfter).toMinutes(); long noOfSeconds = Duration.between(dateBefore, dateAfter).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: 123 Hours: 2972 Minutes: 178346 Seconds: 10700772 |
Further reading:
That’s all about how to find difference two LocalDateTime in Java.