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



描述

java.time.LocalDateTime.until(Temporal endExclusive, TemporalUnit unit) 方法計算在指定單位下距離另一日期和時間的使用時間。

宣告

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

public long until(Temporal endExclusive, TemporalUnit unit)

引數

  • endDateExclusive − 結束日期,排他性,轉換為 LocalDateTime,非空值。

  • unit − 測量時間量的單位,非空值。

返回值

此日期和時間到結束日期和時間之間的用時。

異常

  • DateTimeException − 如果無法計算用時,或無法將結束時間轉換為 LocalDateTime。

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

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

示例

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

package com.tutorialspoint;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

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

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

1758
廣告
© . All rights reserved.