java.time.OffsetTime.until() Method Example



Description

The java.time.OffsetTime.until(Temporal endExclusive, TemporalUnit unit) method calculates the amount of time until another time in terms of the specified unit.

Declaration

Following is the declaration for java.time.OffsetTime.until(Temporal endExclusive, TemporalUnit unit) method.

public long until(Temporal endExclusive, TemporalUnit unit)

Parameters

  • endDateExclusive − the end date, exclusive, which is converted to a OffsetTime, not null.

  • unit − the unit to measure the amount in, not null.

Return Value

the amount of time between this time and the end time.

Exceptions

  • DateTimeException − if the amount cannot be calculated, or the end temporal cannot be converted to a OffsetTime.

  • UnsupportedTemporalTypeException − if the unit is not supported.

  • ArithmeticException − if numeric overflow occurs.

Example

The following example shows the usage of java.time.OffsetTime.until(Temporal endExclusive, TemporalUnit unit) method.

package com.tutorialspoint;

import java.time.OffsetTime;
import java.time.temporal.ChronoUnit;

public class OffsetTimeDemo {
   public static void main(String[] args) {
      
      OffsetTime time = OffsetTime.parse("10:15:30+01:00");
      OffsetTime time1 = OffsetTime.now();
      System.out.println(time.until(time1, ChronoUnit.HOURS));  
   }
}

Let us compile and run the above program, this will produce the following result −

1
Advertisements