Java Convert Milliseconds to Duration
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.
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