Duration ofMinutes() method in Java


The duration can be obtained in a one minute format using the ofMinutes() method in the Duration class in Java. This method requires a single parameter i.e. the number of minutes and it returns the duration in a one minute 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 minutes = 2;
      Duration duration = Duration.ofMinutes(minutes);
      System.out.println("The duration in seconds is: " + duration.getSeconds());
   }
}

Output

The duration in seconds is: 120

Now let us understand the above program.

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

long minutes = 2;
Duration duration = Duration.ofMinutes(minutes);
System.out.println("The duration in seconds is: " + duration.getSeconds());

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

70 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements