ofNanos()
is a Duration
class which is used to get a Duration
class instance representing a number of nanoseconds. The seconds and nanoseconds are extracted from the specified nanoseconds.
The ofNanos
method is defined in the Duration
class. The Duration
class is defined in the java.time
package. To import the Duration
class, check the following import statement.
import java.time.Duration;
public static Duration ofNanos(long nanos)
long nanos
: The number of nanoseconds. It can be positive or negative.The method returns a Duration
object.
In the code below, we create two Duration
class objects with a positive and negative number of nanoseconds. Then we print the objects to the console.
import java.time.Duration;public class Main{public static void main(String[] args) {Duration positiveNonos = Duration.ofNanos(5);System.out.println("Positive Nanoseconds - " + positiveNonos);Duration negativeNonos = Duration.ofNanos(-5);System.out.println("Negative Nanoseconds - " + negativeNonos);}}