Duration ofDays() method in Java

Java 8Object Oriented ProgrammingProgramming

The duration can be obtained in a 24 hour format using the ofDays() method in the Duration class in Java. This method requires a single parameter i.e. the number of days and it returns the duration in a 24 hour format. If the capacity of the duration is exceeded, then the ArithmeticException is thrown.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.time.Duration;
public class Demo {
   public static void main(String[] args) {
      long days = 1;
      Duration duration = Duration.ofDays(days);
      System.out.println("The duration in seconds is: " + duration.getSeconds());
   }
}

Output

The duration in seconds is: 86400

Now let us understand the above program.

The duration in a 24 hour format is obtained using the ofDays() method. Then this duration is printed in seconds using the getSeconds() method. A code snippet that demonstrates this is as follows −

long days = 1;
Duration duration = Duration.ofDays(days);
System.out.println("The duration in seconds is: " + duration.getSeconds());
raja
Published on 26-Mar-2019 05:02:58
Advertisements