What is Duration.getNano() in Java?

getNano() is an instance method of the Duration class used to get the number of nanoseconds stored in the Duration object.

The getNano() method is defined in the Duration class. The Duration class is defined in the java.time package. To import the Duration class, we will use the following import statement:

import java.time.Duration;

Syntax


public long getNano()

Parameters

This method has no parameters.

Return value

This method returns the number of nanoseconds.

Code

In the below code, we will print the number of nanoseconds in the Duration object:

import java.time.Duration;
public class Main{
public static void main(String[] args) {
Duration duration = Duration.ofNanos(1534);
System.out.printf("The number of nanoseconds stored in %s is %s.", duration, duration.getNano());
}
}

Free Resources