Java Calculate Number of Nanoseconds Between two LocalDateTime
Tags: Java LocalDateTime ChronoUnit Java 8
In this Java core tutorial, we learn how to calculate the number of nanoseconds between two LocalDateTime object using the java.time.temporal.ChronoUnit class in Java programming language.
How to get number of nanoseconds between two LocalDateTime in Java
In Java, we can use the ChronoUnit.NANOS.between() to calculate the number of nanoseconds between two LocalDateTime values as the following Java program.
NanosecondsBetweenLocalDateTimeExample.java
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
public class NanosecondsBetweenLocalDateTimeExample {
public static void main(String... args) {
LocalDateTime start = LocalDateTime.parse("2022-07-11T08:30:00");
LocalDateTime end = LocalDateTime.parse("2022-07-11T09:30:00");
// Calculate Number of Nanoseconds Between two LocalDateTime
long numberOfNanoseconds = ChronoUnit.NANOS.between(start, end);
System.out.println(String.format("Number of nanoseconds between %s and %s is %d", start, end, numberOfNanoseconds));
}
}
Number of nanoseconds between 2022-07-11T08:30 and 2022-07-11T09:30 is 3600000000000
Happy Coding 😊
Related Articles
Java Calculate Number of Decades Between two LocalDateTime
Java Calculate Number of Years Between two LocalDateTime
Java Calculate Number of Months Between two LocalDateTime
Java Calculate Number of Weeks Between two LocalDateTime
Java Calculate Number of Days Between two LocalDateTime
Java Calculate Number of Hours Between two LocalDateTime
Java Calculate Number of Minutes Between two LocalDateTime
Java Calculate Number of Seconds Between two LocalDateTime
Java Calculate Number of Milliseconds Between two LocalDateTime