Home »
Java »
Java Reference »
Java Duration Class
Java Duration Class | getNano() Method with Example
Duration Class getNano() method: Here, we are going to learn about the getNano() method of Duration Class with its syntax and example.
Submitted by Preeti Jain, on May 14, 2020
Duration Class getNano() method
- getNano() method is available in java.time package.
- getNano() method is used to return the number of nanoseconds pass in 1 second in this Duration.
- getNano() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- getNano() method does not throw an exception at the time of getting nanoseconds.
Syntax:
public int getNano();
Parameter(s):
Return value:
The return type of this method is int, it returns the number of nanoseconds in 1 second and the duration is defined in some range like [0….999,999,999].
Example:
// Java program to demonstrate the example
// of int getNano() method of Duration
import java.time.*;
public class GetNanoOfDuration {
public static void main(String args[]) {
// Instantiates two Duration objects
Duration du1 = Duration.between(LocalTime.MAX, LocalTime.MIN);
Duration du2 = Duration.between(LocalTime.MIN, LocalTime.MAX);
// Display du1 and du2
System.out.println("du1: " + du1);
System.out.println("du2: " + du2);
// returns the number of nanoseconds within
// the second in this Duration du1
int get_nano_val = du1.getNano();
// Display get_val
System.out.println("du1.getNano(): " + get_nano_val);
// returns the number of nanoseconds
/// within the second in this Duration du2
get_nano_val = du2.getNano();
// Display get_val
System.out.println("du2.getNano(): " + get_nano_val);
}
}
Output
du1: PT-23H-59M-59.999999999S
du2: PT23H59M59.999999999S
du1.getNano(): 1
du2.getNano(): 999999999