Java Convert Number of Milliseconds to Nanoseconds
Tags: Java Duration Java 8
In this Java tutorial we learn how to convert a number of milliseconds to nanoseconds using the java.time.Duration class in Java programming language.
How to convert number of milliseconds to nanoseconds in Java
In Java, with a given number of milliseconds we can use the Duration.toNanos() method to convert it to nanoseconds value as the following Java program.
ConvertMillisecondsToNanosecondsExample.java
import java.time.Duration;
public class ConvertMillisecondsToNanosecondsExample {
public static void main(String... args) {
long numberOfMilliseconds = 1;
// Convert Number of Milliseconds to Nanoseconds
long numberOfNanoseconds = Duration.ofMillis(numberOfMilliseconds).toNanos();
System.out.println("Number of milliseconds: " + numberOfMilliseconds);
System.out.println("Number of nanoseconds: " + numberOfNanoseconds);
}
}
Number of milliseconds: 1
Number of nanoseconds: 1000000
Happy Coding 😊
Related Articles
Java Convert Number of Minutes to Seconds
Java Convert Number of Minutes to Milliseconds
Java Convert Number of Minutes to Nanoseconds
Java Convert Number of Seconds to Milliseconds
Java Convert Number of Seconds to Nanoseconds
Java Convert Number of Days to Nanoseconds
Java Convert Number of Hours to Minutes
Java Convert Number of Hours to Seconds