Duration between() method in Java

Java 8ProgrammingObject Oriented Programming

The duration between two temporal objects can be obtained using the method between() in the Duration class in Java. This method requires two parameters i.e. the start duration and the end duration. Also, it returns the duration between these two temporal duration objects.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.time.*;
public class Demo {
   public static void main(String[] args) {
      Duration d = Duration.between(LocalTime.MIDNIGHT, LocalTime.NOON);
      System.out.println("The duration between the two in seconds is: " + d.getSeconds());
   }
}

Output

The duration between the two in seconds is: 43200

Now let us understand the above program.

The duration between the two temporal objects is obtained using the method between() and then this is printed. A code snippet that demonstrates this is as follows −

public static void main(String[] args) {
   Duration d = Duration.between(LocalTime.MIDNIGHT, LocalTime.NOON);
   System.out.println("The duration between the two in seconds is: " + d.getSeconds());
}
raja
Updated on 30-Jul-2019 22:30:25

Advertisements