Home »
Java »
Java Reference »
Java Duration Class
Java Duration Class | ofNanos() Method with Example
Duration Class ofNanos() method: Here, we are going to learn about the ofNanos() method of Duration Class with its syntax and example.
Submitted by Preeti Jain, on May 16, 2020
Duration Class ofNanos() method
- ofNanos() method is available in java.time package.
- ofNanos() method is used to represent the given nanoseconds value in this Duration.
- ofNanos() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
- ofNanos() method may throw an exception at the time of representing nanoseconds.
ArithmeticException: This exception may throw when the given nanoseconds value exceeds the length of this Duration.
Syntax:
public static Duration ofNanos(long nanos_val);
Parameter(s):
- long nanos_val – represents the number of nanoseconds in value.
Return value:
The return type of this method is Duration, it returns the Duration that holds the value of the given nanoseconds.
Example:
// Java program to demonstrate the example
// of ofNanos(long nanos_val) method of Duration
import java.time.*;
public class OfNanosOfDuration {
public static void main(String args[]) {
long nanos_val = 59990000;
// returns the Duration that holds
// the value of the given argument i.e.
// here we are representing
// nanoseconds in standard seconds in
// this Duration du
Duration du = Duration.ofNanos(nanos_val);
// Display du
System.out.println("Duration.ofNanos(nanos_val): " + du);
}
}
Output
Duration.ofNanos(nanos_val): PT0.05999S