In this article, we will discuss what are the different ways to calculate difference between 2 Date/time
Ways to find difference between 2 Date/time :
- Using Period – to find difference between 2 Dates
- Using Duration – to find difference between 2 Time
- Using ChronoUnit – to find difference between 2 date/time in terms of Years, Minutes, Days, Hours, Minutes, Seconds and Nanos
1. Period – find difference between 2 Dates :
- Period class has many useful methods –
- Period.between() method helps to find difference between 2 LocalDate/Instant
- Period.between() method accepts 2 input-arguments – both LocalDate and returns difference after calculation in PxYyMzD format where –
- P indicates Period of difference between 2 Dates
- Y indicates Years
- M indicates Months
- D indicates Days
- x, y, z indicates integer numbers for Years, Months and Days respectively
- If the requirement is to find difference between LocalDateTime/ZonedDateTime/OffsetDateTime then convert into LocalDate using toLocalDate() method and use Period.between() method to find difference
FindPeriodBetweenTwoDates.java
package in.bench.resources.java8.period.duration.examples;
import java.time.LocalDate;
import java.time.Month;
import java.time.Period;
public class FindPeriodBetweenTwoDates {
public static void main(String[] args) {
// 1. Joining date
LocalDate localDate1 = LocalDate.of(2017, Month.MARCH, 6);
System.out.println("Joining date is :- \n" + localDate1);
// 2. Relieving date
LocalDate localDate2 = LocalDate.of(2019, Month.JUNE, 28);
System.out.println("\nRelieving date is :- \n" + localDate2);
// 3. difference between 2 LocalDate
Period period = Period.between(localDate1, localDate2);
System.out.println("\nDifference between Joining & Relieving Dates is :- \n"
+ period);
// 3.1 get year part
int years = period.getYears();
System.out.println("\nYear part from the difference is :- \n"
+ years);
// 3.2 get month part
int months = period.getMonths();
System.out.println("\nMonth part from the difference is :- \n"
+ months);
// 3.3 get day part
int days = period.getDays();
System.out.print("\nDay part from the difference is :- \n"
+ days);
}
}
Output:
Joining date is :-
2017-03-06
Relieving date is :-
2019-06-28
Difference between Joining & Relieving Dates is :-
P2Y3M22D
Year part from the difference is :-
2
Month part from the difference is :-
3
Day part from the difference is :-
22
2. Duration – find difference between 2 Time :
- Duration class has many useful methods –
- Duration.between() method helps to find difference between 2 LocalTime
- Duration.between() method accepts 2 input-arguments – both LocalTime and returns difference after calculation in PTxHyMz.nS format where –
- PT indicates Duration of difference between 2 Time Period
- H indicates Hours
- M indicates Minutes
- S indicates Seconds
- x, y, z, n indicates integer numbers for Hours, Minutes Seconds and Nanos respectively
- If the requirement is to find difference between LocalDateTime/ZonedDateTime/OffsetDateTime/OffsetTime then convert into LocalTime using toLocalTime() method and use Duration.between() method to find difference
FindDurationBetweenTwoTime.java
package in.bench.resources.java8.period.duration.examples;
import java.time.Duration;
import java.time.LocalTime;
public class FindDurationBetweenTwoTime {
public static void main(String[] args) {
// 1. start time
LocalTime localTime1 = LocalTime.of(8, 29, 59, 123);
System.out.println("Start time is :- \n" + localTime1);
// 2. end time
LocalTime localTime2 = LocalTime.of(17, 59, 29, 321);
System.out.println("\nEnd time is :- \n" + localTime2);
// 3. difference between 2 Time
Duration duration = Duration.between(localTime1, localTime2);
System.out.println("\nDifference between Start & End Time is :- \n"
+ duration);
// 3.1 convert Duration difference into seconds leaving nano
long seconds = duration.getSeconds();
System.out.print("\nNumber of Seconds from the difference is :- \n"
+ seconds);
}
}
Output:
Start time is :-
08:29:59.000000123
End time is :-
17:59:29.000000321
Difference between Start & End Time is :-
PT9H29M30.000000198S
Number of Seconds from the difference is :-
34170
3. ChronoUnit – find difference between 2 Date/time :
- ChronoUnit helps to find difference of 2 Date/time in terms of Years, Months, Days, Hours, Minutes, Seconds & Nanos and returns difference value in long format
- ChronoUnit.YEARS.between(ldt1, ldt2) – calculates difference in terms of Years
- ChronoUnit.MONTHS.between(ldt1, ldt2) – calculates difference in terms of Months
- ChronoUnit.DAYS.between(ldt1, ldt2) – calculates difference in terms of Days
- ChronoUnit.HOURS.between(ldt1, ldt2) – calculates difference in terms of Hours
- ChronoUnit.MINUTES.between(ldt1, ldt2) – calculates difference in terms of Minutes
- ChronoUnit.SECONDS.between(ldt1, ldt2) – calculates difference in terms of Seconds
- ChronoUnit.NANOS.between(ldt1, ldt2) – calculates difference in terms of Nanos
FindDifferenceUsingChronoUnits.java
package in.bench.resources.java8.period.duration.examples;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.temporal.ChronoUnit;
public class FindDifferenceUsingChronoUnits {
public static void main(String[] args) {
// Joining date/time
LocalDateTime localDateTime1 = LocalDateTime
.of(2017, Month.MARCH, 6, 8, 29, 59, 123);
System.out.println("Joining date/time is :- \n" + localDateTime1);
// Relieving date/time
LocalDateTime localDateTime2 = LocalDateTime
.of(2019, Month.JUNE, 28, 17, 59, 29, 321);
System.out.println("\nRelieving date/time is :- \n" + localDateTime2);
// 1. difference between 2 LocalDateTime in Years
long years = ChronoUnit.YEARS.between(localDateTime1, localDateTime2);
System.out.println("\nDifference in Years is :- \n" + years);
// 2. difference between 2 LocalDateTime in Months
long months = ChronoUnit.MONTHS.between(localDateTime1, localDateTime2);
System.out.println("\nDifference in Months is :- \n" + months);
// 3. difference between 2 LocalDateTime in Days
long days = ChronoUnit.DAYS.between(localDateTime1, localDateTime2);
System.out.println("\nDifference in Days is :- \n" + days);
// 4. difference between 2 LocalDateTime in Hours
long hours = ChronoUnit.HOURS.between(localDateTime1, localDateTime2);
System.out.println("\nDifference in Hours is :- \n" + hours);
// 5. difference between 2 LocalDateTime in Minutes
long minutes = ChronoUnit.MINUTES.between(localDateTime1, localDateTime2);
System.out.println("\nDifference in Minutes is :- \n" + minutes);
// 6. difference between 2 LocalDateTime in Seconds
long seconds = ChronoUnit.SECONDS.between(localDateTime1, localDateTime2);
System.out.println("\nDifference in Seconds is :- \n" + seconds);
// 7. difference between 2 LocalDateTime in Nanoseconds
long nanoseconds = ChronoUnit.NANOS.between(localDateTime1, localDateTime2);
System.out.print("\nDifference in Nanos is :- \n" + nanoseconds);
}
}
Output:
Joining date/time is :-
2017-03-06T08:29:59.000000123
Relieving date/time is :-
2019-06-28T17:59:29.000000321
Difference in Years is :-
2
Difference in Months is :-
27
Difference in Days is :-
844
Difference in Hours is :-
20265
Difference in Minutes is :-
1215929
Difference in Seconds is :-
72955770
Difference in Nanos is :-
72955770000000198
Related Articles :
- Java 8 – How to find difference between two LocalDate instances using Period ?
- Java 8 – How to calculate number of Days between two LocalDate instances ?
- Java 8 – How to find time duration between two LocalTime instances ?
- Java 9 – Find difference between two LocalTime instances upto nanosecond precision ?
- Java 8 – How to find difference between two LocalDateTime using Period & Duration ?
- Java 9 – Find difference between two LocalDateTime instances upto nanosecond precision ?
- Java 8 – How to find difference between two ZonedDateTime using Period & Duration ?
- Java 9 – Find difference between two ZonedDateTime instances upto nanosecond precision ?
- Java 8 – How to find difference between two OffsetDateTime using Period & Duration ?
- Java 9 – Find difference between two OffsetDateTime instances upto nanosecond precision ?
- Java 8 – How to find difference between two Instant instances using Duration ?
- Java 9 – Find difference between two Instant instances upto nanosecond precision ?
References :
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/Period.html
- https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html
- https://docs.oracle.com/javase/8/docs/api/java/time/temporal/ChronoUnit.html
Happy Coding !!
Happy Learning !!