In this article, we will learn how to find difference between two Instant instances using different methods of Duration class provided in Java 1.8 version
Find difference between two Instant instances :
- 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,
- toDays() – gets the number of days in this duration
- toHours() – gets the number of hours in this duration
- toMinutes() – gets the number of minutes in this duration
- getSeconds() – gets the number of seconds in this duration
- toMillis() – converts this duration to the total length in milliseconds
- getNano() – gets the number of nanoseconds within the second in this duration
- Finally print difference in terms of days, hours, minutes, seconds, milliseconds, nanoseconds to the console
FindDifferenceOfTwoInstant.java
package in.bench.resources.java8.instant.examples;
import java.time.Duration;
import java.time.Instant;
public class FindDifferenceOfTwoInstant {
public static void main(String[] args) {
// 1. organization joining Instant
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 Instant
String relievingInstantInStr = "2019-06-28T20:31:43.933345200Z";
Instant relievingInstant = Instant.parse(relievingInstantInStr);
System.out.println("\nOrganization Relieving Instant is :- \n" + relievingInstant);
// 4. difference between 2 Instant - Time duration spent in Office
Duration duration = Duration.between(
joiningInstant,
relievingInstant
);
System.out.println("\nDuration between 2 Instant is :- \n" + duration);
// 5. get difference in days/hours/minutes/seconds/millis/nanos
long days = duration.toDays();
long hours = duration.toHours();
long minutes = duration.toMinutes();
long seconds = duration.getSeconds();
long millis = duration.toMillis();
long nanos = duration.toNanos();
// 5.1 print number of Days between 2 Instant
System.out.println("\nDays between 2 Instant "
+ "leaving hours/minutes/seconds/millis/nanos is :- \n"
+ days + " days");
// 5.2 print number of Hours between 2 Instant
System.out.println("\nHours between 2 Instant "
+ "leaving minutes/seconds/millis/nanos is :- \n"
+ hours + " hours");
// 5.3 print number of Minutes between 2 Instant
System.out.println("\nMinutes between 2 Instant "
+ "leaving seconds/millis/nanos is :- \n"
+ minutes + " minutes");
// 5.4 print number of Seconds between 2 Instant
System.out.println("\nSeconds between 2 Instant "
+ "leaving millis/nanos is :- \n"
+ seconds + " seconds");
// 5.5 print number of Milliseconds between 2 Instant
System.out.println("\nMilliseconds between 2 Instant "
+ "leaving nanos is :- \n"
+ millis + " milliseconds");
// 5.6 print number of Nanoseconds between 2 Instant
System.out.print("\nNanoseconds between 2 Instant is :- \n"
+ nanos + " nanoseconds");
}
}
Output:
Organization Joining Instant is :-
2017-03-06T10:32:56.308075900Z
Organization Relieving Instant is :-
2019-06-28T20:31:43.933345200Z
Duration between 2 Instant is :-
PT20265H58M47.6252693S
Days between 2 Instant leaving hours/minutes/seconds/millis/nanos is :-
844 days
Hours between 2 Instant leaving minutes/seconds/millis/nanos is :-
20265 hours
Minutes between 2 Instant leaving seconds/millis/nanos is :-
1215958 minutes
Seconds between 2 Instant leaving millis/nanos is :-
72957527 seconds
Milliseconds between 2 Instant leaving nanos is :-
72957527625 milliseconds
Nanoseconds between 2 Instant is :-
72957527625269300 nanoseconds
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/8/docs/api/java/time/Instant.html
- https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!