Java Calculate Number of Nanoseconds Between two LocalTime
Tags: Java LocalTime ChronoUnit Java 8
In this Java core tutorial, we learn how to calculate the number of nanoseconds between two LocalTime objects using the java.time.temporal.ChronoUnit class in Java programming language.
How to get number of nanoseconds between two LocalTime
In Java, we can use the ChronoUnit.NANOS.between() method to calculate the number of nanoseconds between two LocalTime values as the example Java code below.
NanosecondsBetweenLocalTimeExample.java
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
public class NanosecondsBetweenLocalTimeExample {
public static void main(String... args) {
LocalTime startTime = LocalTime.of(10, 20, 30);
LocalTime endTime = LocalTime.of(10, 30, 40);
// Calculate Number of Nanoseconds Between two LocalTime
long numberOfNanoseconds = ChronoUnit.NANOS.between(startTime, endTime);
System.out.println(String.format("Number of nanoseconds between %s and %s is %d", startTime, endTime, numberOfNanoseconds));
}
}
Number of nanoseconds between 10:20:30 and 10:30:40 is 610000000000
Happy Coding 😊
Related Articles
Java Calculate Number of Hours Between two LocalTime
Java Calculate Number of Minutes Between two LocalTime