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.