Java 9 – Find difference between two Instant instances upto nanosecond precision ?

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:

References:

Happy Coding !!
Happy Learning !!

Java 9 – How to convert Instant to LocalDate using ofInstant() method ?
Java 8 – How to find difference between two Instant instances using Duration ?