Open In App

Duration between(Temporal, Temporal) method in Java with Examples

The between(Temporal) method of Duration Class in java.time package is used to get a Duration in between the two Temporal objects passed as the parameter. The first parameter is inclusive whereas the second parameter is exclusive of the calculation. If the objects are of different types, then the duration is calculated based on the type of the first object.

Syntax:



public static Duration between(Temporal startDuration, Temporal endDuration)

Parameters: This method accepts two parameters:

Return Value: This method returns a Duration representing the time passed in between the instants passed as the parameters.



Exception: This method throws:

Below examples illustrate the Duration.between() method:

Example 1:




// Java code to illustrate between() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Duration using between() method
        Duration duration
            = Duration.between(LocalTime.MIDNIGHT,
                               LocalTime.NOON);
  
        System.out.println(duration.getSeconds());
    }
}

Output:
43200

Example 2:




// Java code to illustrate between() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Duration using between() method
        Duration duration
            = Duration.between(LocalTime.NOON,
                               LocalTime.MAX);
  
        System.out.println(duration.getSeconds());
    }
}

Output:
43199

Reference: Oracle Doc


Article Tags :