Java Program to convert this duration to the total length in milliseconds

Java 8Object Oriented ProgrammingProgramming

With this, get the milliseconds in days, hours and minutes. At first, set the Duration −

Duration d1 = Duration.ofDays(20);
Duration d2 = Duration.ofHours(100);
Duration d3 = Duration.ofMinutes(150);

Convert the above Duration to nanoseconds −

System.out.println("Milliseconds in 20 days = "+d1.toMillis());
System.out.println("Milliseconds in 100 hours = "+d2.toMillis());
System.out.println("Milliseconds in 150 minutes = "+d3.toMillis());

Example

 Live Demo

import java.time.Duration;
public class Demo {
   public static void main(String[] args) {
      Duration d1 = Duration.ofDays(20);
      Duration d2 = Duration.ofHours(100);
      Duration d3 = Duration.ofMinutes(150);
      System.out.println("Milliseconds in 20 days = "+d1.toMillis());
      System.out.println("Milliseconds in 100 hours = "+d2.toMillis());
      System.out.println("Milliseconds in 150 minutes = "+d3.toMillis());
   }
}

Output

Milliseconds in 20 days = 1728000000
Milliseconds in 100 hours = 360000000
Milliseconds in 150 minutes = 9000000
raja
Published on 25-Apr-2019 08:53:28
Advertisements