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

Java 8Object Oriented ProgrammingProgramming

With this, get the nanoseconds in days, hours and minutes. At first, set the Duration:

Duration d1 = Duration.ofDays(5);
Duration d2 = Duration.ofHours(20);
Duration d3 = Duration.ofMinutes(15);

Convert the above Duration to nanoseconds:

System.out.println("Nanoseconds in 5 days = "+d1.toNanos());
System.out.println("Nanoseconds in 20 hours = "+d2.toNanos());
System.out.println("Nanoseconds in 15 minutes = "+d3.toNanos());

Example

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

Output

Nanoseconds in 5 days = 432000000000000
Nanoseconds in 20 hours = 72000000000000
Nanoseconds in 15 minutes = 900000000000
raja
Published on 24-Apr-2019 11:47:49
Advertisements