Java Calculate Number of Nanoseconds Between two ZonedDateTime
Tags: Java ZonedDateTime ChronoUnit Java 8
In this Java core tutorial, we learn how to calculate the number of nanoseconds between two ZonedDateTime objects using the java.time.temporal.ChronoUnit class in Java programming language.
How to get number of nanoseconds between two ZonedDateTime in Java
In Java, to calculate the number of nanoseconds between two ZonedDateTime value, we can use the ChronoUnit.NANOS.between() method as the following example Java code.
NanosecondsBetweenZonedDateTimeExample.java
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
public class NanosecondsBetweenZonedDateTimeExample {
public static void main(String... args) {
ZonedDateTime startZonedDateTime = ZonedDateTime.parse("2022-01-01T08:15:30+10:00[Australia/Sydney]");
ZonedDateTime endZonedDateTime = ZonedDateTime.parse("2022-01-01T08:15:35+10:00[Australia/Sydney]");
// Calculate Number of Nanoseconds Between two ZonedDateTime
long numberOfNanoseconds = ChronoUnit.NANOS.between(startZonedDateTime, endZonedDateTime);
System.out.println(String.format("Number of nanoseconds between %s and %s is %d",
startZonedDateTime,
endZonedDateTime,
numberOfNanoseconds));
}
}
Number of nanoseconds between 2022-01-01T09:15:30+11:00[Australia/Sydney] and 2022-01-01T09:15:35+11:00[Australia/Sydney] is 5000000000
Happy Coding 😊
Related Articles
Java Calculate Number of Decades Between two ZonedDateTime
Java Calculate Number of Years Between two ZonedDateTime
Java Calculate Number of Months Between two ZonedDateTime
Java Calculate Number of Weeks Between two ZonedDateTime
Java Calculate Number of Days Between two ZonedDateTime
Java Calculate Number of Hours Between two ZonedDateTime
Java Calculate Number of Minutes Between two ZonedDateTime
Java Calculate Number of Seconds Between two ZonedDateTime
Java Calculate Number of Milliseconds Between two ZonedDateTime