In this article, we will learn how to find difference between two LocalDateTime instances up-to Nanosecond precision using newly introduced methods in Java 1.9 version
Find difference between two LocalDateTime instances up-to Nano precision :
- To find difference between two LocalDateTime instances, first we need to segregate Date & Time parts as there is no way to find difference between them directly
- To segregate Date and Time parts separately, we can use toLocalDate() and toLocalTime() methods of LocalDateTime
- toLocalDate() – gets the
LocalDate
part of invoking LocalDateTime - toLocalTime() – gets the
LocalTime
part of invoking LocalDateTime
- toLocalDate() – gets the
- Use Period.between() method to find difference between 2 LocalDate instances
- Period.between() method accepts 2 input-arguments and returns difference of two LocalDate,
- Pass organization joining date as 1st argument
- Pass organization relieving date as 2nd argument
- Period class has many useful method like,
- getYears() – returns numbers of years between 2 LocalDate instances
- getMonths() – returns numbers of months between 2 LocalDate instances
- getDays() – returns numbers of days between 2 LocalDate instances
- Period.between() method accepts 2 input-arguments and returns difference of two LocalDate,
- Similarly, use Duration.between() method to find difference between 2 LocalTime instances
- Duration.between() method accepts 2 input-arguments and returns difference of two LocalTime,
- Pass office start time as 1st argument
- Pass office end time as 2nd argument
- Duration class has many useful methods like,
- toHoursPart() – Extracts the number of hours part in the duration
- toMinutesPart() – Extracts the number of minutes part in the duration
- toSecondsPart() – Extracts the number of seconds part in the duration
- toMillisPart() – Extracts the number of milliseconds part of the duration
- toNanosPart() – get the nanoseconds part within seconds of the duration
- Duration.between() method accepts 2 input-arguments and returns difference of two LocalTime,
- Finally print Time Period/Duration spent in an organization to the console
FindDifferenceOfTwoLocalDateTime.java
package in.bench.resources.java9.conversion;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Period;
public class FindDifferenceOfTwoLocalDateTime {
public static void main(String[] args) {
// 1. organization joining date
LocalDateTime joiningLocalDateTime = LocalDateTime.of(2017, 3, 6, 10, 32, 56, 123456789);
System.out.println("Organization Joining Date/time is :- \n" + joiningLocalDateTime);
// 2. organization relieving date
LocalDateTime relievingLocalDateTime = LocalDateTime.of(2019, 6, 28, 20, 31, 43, 987654321);
System.out.println("\nOrganization Relieving Date/time is :- \n" + relievingLocalDateTime);
// 3. difference between 2 LocalDate - Days spent in a organization
Period period = Period.between(
joiningLocalDateTime.toLocalDate(),
relievingLocalDateTime.toLocalDate()
);
System.out.println("\nPeriod between 2 LocalDate is :- \n" + period);
// 3.1 get difference of years/months/days
int years = period.getYears();
int months = period.getMonths();
int days = period.getDays();
// 4. difference between 2 LocalTime - Time duration spent in Office
Duration duration = Duration.between(
joiningLocalDateTime.toLocalTime(),
relievingLocalDateTime.toLocalTime()
);
System.out.println("\nTime Duration between 2 LocalTime is :- \n" + duration);
// 4.1 get difference in hours/minutes/seconds/millis/nanos
int hours = duration.toHoursPart();
int minutes = duration.toMinutesPart();
long seconds = duration.toSecondsPart();
int millis = duration.toMillisPart();
int nanos = duration.toNanosPart();
// 5. print to console
System.out.print("\nPeriod & Time Duration spent in an Organization is :- \n"
+ years + " Years "
+ months + " Months "
+ days + " Days "
+ hours + " Hours "
+ minutes + " Minutes "
+ seconds + " Seconds "
+ millis + " Millisecond "
+ nanos + " Nanosecond");
}
}
Output:
Organization Joining Date/time is :-
2017-03-06T10:32:56.123456789
Organization Relieving Date/time is :-
2019-06-28T20:31:43.987654321
Period between 2 LocalDate is :-
P2Y3M22D
Time Duration between 2 LocalTime is :-
PT9H58M47.864197532S
Period & Time Duration spent in an Organization is :-
2 Years 3 Months 22 Days 9 Hours 58 Minutes 47 Seconds 864 Millisecond 864197532 Nanosecond
Related Articles:
- Java 8 – ZonedDateTime with method details and examples
- Java 8 – How to get Date, Time and Zone fields from ZonedDateTime ?
- Java 8 – How to form ZonedDateTime passing Date, Time and Zone fields ?
- Java 8 – How to form ZonedDateTime passing LocalDate, LocalTime and ZoneId ?
- Java 8 – How to form ZonedDateTime passing LocalDateTime and ZoneId ?
- Java 8 – How to form ZonedDateTime passing Instant and ZoneId ?
- Java 8 – How to parse ZonedDateTime in String form ?
- Java 8 – How to convert String to ZonedDateTime ?
- Java 8 – How to convert ZonedDateTime to String ?
- Java 8 – How to convert ZonedDateTime in different formats ?
- Java 8 – How to convert ZonedDateTime in different Format Style ?
- Java 8 – How to convert ZonedDateTime to LocalDateTime ?
- Java 8 – How to convert ZonedDateTime to an OffsetDateTime ?
- Java 8 – How to convert ZonedDateTime to an Instant ?
- Java 8 – How to extract LocalDate and LocalTime and LocalDateTime from ZonedDateTime ?
- Java 8 – How to convert ZonedDateTime to java.util.Date and vice-versa ?
- Java 8 – How to convert ZonedDateTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert ZonedDateTime to Calendar and vice-versa ?
- Java 8 – How to convert ZonedDateTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert ZonedDateTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to ZonedDateTime in different ways ?
- Java 8 – How to add Date and Time fields to ZonedDateTime ?
- Java 8 – How to subtract Date and Time fields from ZonedDateTime ?
- Java 8 – How to alter Date, Time and Zone fields of ZonedDateTime ?
- Java 8 – How to check whether a ZonedDateTime is Before another ZonedDateTime ?
- Java 8 – How to check whether a ZonedDateTime is After another ZonedDateTime ?
- Java 8 – How to compare two ZonedDateTime instances ?
- Java 8 – How to find difference between two ZonedDateTime using Period & Duration ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/9/docs/api/java/time/LocalDateTime.html
- https://docs.oracle.com/javase/9/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/9/docs/api/java/time/LocalTime.html
- https://docs.oracle.com/javase/9/docs/api/java/time/Period.html
- https://docs.oracle.com/javase/9/docs/api/java/time/Duration.html
- https://docs.oracle.com/javase/9/docs/api/java/time/ZoneId.html
- https://docs.oracle.com/javase/9/docs/api/java/time/ZoneOffset.html
Happy Coding !!
Happy Learning !!