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



說明

java.time.OffsetDateTime.until(Temporal endExclusive, TemporalUnit unit) 方法計算與另一個日期時間對應的指定單位的時間量。

宣告

以下是java.time.OffsetDateTime.until(Temporal endExclusive, TemporalUnit unit) 方法的宣告。

public long until(Temporal endExclusive, TemporalUnit unit)

引數

  • endDateExclusive - 結束日期,不包括,將轉換為 OffsetDateTime,非 null。

  • unit - 用來測量時間量,非 null。

返回的值

此日期時間和結束日期時間之間的持續時間。

異常

  • DateTimeException - 如果無法計算時間量,或者結束時間無法轉換為 OffsetDateTime。

  • UnsupportedTemporalTypeException - 如果不支援該單位。

  • ArithmeticException - 如果發生數字溢位。

示例

以下示例展示了 java.time.OffsetDateTime.until(Temporal endExclusive, TemporalUnit unit) 方法的用法。

package com.tutorialspoint;

import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;

public class OffsetDateTimeDemo {
   public static void main(String[] args) {
      
      OffsetDateTime date = OffsetDateTime.parse("2017-01-03T10:15:30+01:00");
      OffsetDateTime date1 = OffsetDateTime.now();
      System.out.println(date.until(date1, ChronoUnit.HOURS));  
   }
}

讓我們編譯並執行上述程式,這將產生以下結果 -

8566
廣告
© . All rights reserved.