Java Convert Milliseconds to Duration
Tags: Java Duration Java 8
In this Java core tutorial we learn how to convert a number of milliseconds to java.time.Duration object in Java programming language.
How to convert milliseconds to Duration in Java
In Java, we can use the Duration.ofMillis(long millis) static method to instantiate a Duration object from number of milliseconds as the example Java code below.
ConvertMillisecondsToDurationExample1.java
import java.time.Duration;
public class ConvertMillisecondsToDurationExample1 {
public static void main(String... args) {
long numberOfMilliseconds = 20000;
// Convert number of milliseconds to Duration
Duration duration = Duration.ofMillis(numberOfMilliseconds);
System.out.println("Number of milliseconds: " + numberOfMilliseconds);
System.out.println("Duration: " + duration);
}
}
Number of milliseconds: 20000
Duration: PT20S
Happy Coding 😊
Related Articles
Java Convert Hours to Duration
Java Convert Minutes to Duration
Java Convert Seconds to Duration
Java Convert Nanoseconds to Duration