Java 8 – How to find difference between two Instant instances using Duration ?

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:

References:

Happy Coding !!
Happy Learning !!

Java 9 – Find difference between two Instant instances upto nanosecond precision ?
Java 8 – How to compare two Instant instances ?