java.time.OffsetTime.until()方法示例



说明

java.time.OffsetTime.until(Temporal endExclusive, TemporalUnit unit) 方法计算以指定单位表示的时间的持续时间,截止到另一个时间点结束。

声明

以下是 java.time.OffsetTime.until(Temporal endExclusive, TemporalUnit unit) 方法的声明。

public long until(Temporal endExclusive, TemporalUnit unit)

参数

  • endDateExclusive − 结束日期,不包括,它会被转换为 OffsetTime,不能为空。

  • unit − 测量持续时间所用的单位,不能为空。

返回值

此时间与结束时间之间的时间持续时间。

异常

  • DateTimeException − 如果无法计算持续时间,或者无法将结束时间转换为 OffsetTime.

  • UnsupportedTemporalTypeException − 如果不支持此单位。

  • ArithmeticException − 如果发生数字上溢出。

示例

以下示例显示了如何使用 java.time.OffsetTime.until(Temporal endExclusive, TemporalUnit unit) 方法。

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));  
   }
}

编译并运行上面的程序,将生成以下结果 −

1
广告
© . All rights reserved.