In this article, we will learn how to find difference between two Instant instances using different methods of Duration class provided in Java 1.9 version
Difference between 2 Instant up-to Nanosecond precision :
- To find difference between two Instant instances, use Duration.between() method to calculate difference in terms of days, hours, minutes, seconds, milliseconds, nanoseconds
- Duration.between() method accepts 2 input-arguments as Instant and returns difference of two Instant,
- Pass organization joining as 1st Instant
- Pass organization relieving as 2nd Instant
- Duration class has many useful methods like,
- toDaysPart() – Extracts the number of days in the duration
- 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
- Finally print difference in terms of days, hours, minutes, seconds, milliseconds, nanoseconds to the console
FindDifferenceOfTwoInstant.java
package in.bench.resources.java9.conversion;
import java.time.Duration;
import java.time.Instant;
public class FindDifferenceOfTwoInstant {
public static void main(String[] args) {
// 1. organization joining date
String joiningInstantInStr = "2017-03-06T10:32:56.308075900Z";
Instant joiningInstant = Instant.parse(joiningInstantInStr);
System.out.println("Organization Joining Instant is :- \n" + joiningInstant);
// 2. organization relieving date
String relievingInstantInStr = "2019-06-28T20:31:43.933345200Z";
Instant relievingInstant = Instant.parse(relievingInstantInStr);
System.out.println("\nOrganization Relieving Instant is :- \n" + relievingInstant);
// 3. difference between 2 LocalTime - Time duration spent in Office
Duration duration = Duration.between(
joiningInstant,
relievingInstant
);
System.out.println("\nDuration between 2 Instants is :- \n" + duration);
// 4. get difference in days/hours/minutes/seconds/millis/nanos
long days = duration.toDaysPart();
int hours = duration.toHoursPart();
int minutes = duration.toMinutesPart();
int 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"
+ days + " Days "
+ hours + " Hours "
+ minutes + " Minutes "
+ seconds + " Seconds "
+ millis + " Millis "
+ nanos + " Nanos");
}
}
Output:
Organization Joining Instant is :-
2017-03-06T10:32:56.308075900Z
Organization Relieving Instant is :-
2019-06-28T20:31:43.933345200Z
Duration between 2 Instants is :-
PT20265H58M47.6252693S
Period & Time Duration spent in an Organization is :-
844 Days 9 Hours 58 Minutes 47 Seconds 625 Millis 625269300 Nanos
Related Articles:
- Java 8 – Instant with method details and examples
- Java 8 – How to get instantaneous moment at UTC/GMT using Instant ?
- Java 8 – How to form an Instant passing Seconds and Nanoseconds fields ?
- Java 8 – How to get Seconds and Nanoseconds from an Instant ?
- Java 8 – How to parse Instant in String form ?
- Java 8 – How to convert Instant to LocalDate ?
- Java 8 – How to convert Instant to LocalTime ?
- Java 8 – How to convert Instant to LocalDateTime ?
- Java 8 – How to convert Instant to ZonedDateTime ?
- Java 8 – How to convert Instant to an OffsetDateTime ?
- Java 8 – How to convert Instant to number of Seconds and vice-versa ?
- Java 8 – How to convert Instant to number of Milliseconds and vice-versa ?
- Java 8 – How to convert Instant to java.util.Date and vice-versa ?
- Java 8 – How to convert Instant to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert Instant to Calendar and vice-versa ?
- Java 8 – How to convert Instant to GregorianCalendar and vice-versa ?
- Java 8 – How to convert Instant to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to an Instant in different ways ?
- Java 8 – How to add Second, Millisecond and Nanosecond to an Instant ?
- Java 8 – How to subtract Second, Millisecond and Nanosecond from an Instant ?
- Java 8 – How to check whether an Instant is Before another Instant ?
- Java 8 – How to check whether an Instant is After another Instant ?
- Java 8 – How to compare two Instant instances ?
- Java 8 – How to find difference between two Instant instances using Duration ?
- Java 9 – Find difference between two Instant instances upto nanosecond precision ?
- Java 9 – How to convert Instant to LocalDate using ofInstant() method ?
- Java 9 – How to convert Instant to LocalTime using ofInstant() method ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/9/docs/api/java/time/Duration.html
- https://docs.oracle.com/javase/9/docs/api/java/time/Instant.html
- https://docs.oracle.com/javase/9/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!