Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
tonanos
duration

What is Duration.toNanos() in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

toNanos() is an instance method of the Duration class used to convert the duration to the total length in nanoseconds (expressed as a long).

The toNanos() 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;

Syntax


public long toNanos()

Parameters

The method has no parameters.

Return value

This method returns the total length of the duration in nanoseconds.

Code

In the code below, Duration class objects of different time units are converted to nanoseconds.

import java.time.Duration;
public class Main{
public static void main(String[] args) {
Duration durationInMilliSeconds = Duration.ofMillis(10000);
System.out.println("100 milliseconds in Nanoseconds - " + durationInMilliSeconds.toNanos());
Duration durationInMinutes = Duration.ofMinutes(10);
System.out.println("10 minutes in Nanoseconds - " + durationInMinutes.toNanos());
}
}

RELATED TAGS

java
communitycreator
tonanos
duration

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring