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



描述

java.time.Year.until(Temporal endExclusive, TemporalUnit unit) 方法按照指定單位,計算到另一個日期的時間量。

宣告

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

public long until(Temporal endExclusive, TemporalUnit unit)

引數

  • endDateExclusive − 將 end date(非包容)轉換為 Year,不能為空。

  • unit − 用來測量數量的單位,不能為空。

返回值

此日期和 end date 之間的時間量。

異常

  • DateTimeException − 如果無法計算數量,或者無法將 end temporal 轉換為 Year。

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

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

示例

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

package com.tutorialspoint;

import java.time.Year;
import java.time.temporal.ChronoUnit;

public class YearDemo {
   public static void main(String[] args) {

      Year date = Year.parse("2015");
      Year date1 = Year.now();
      System.out.println(date.until(date1, ChronoUnit.YEARS));  
   }
}

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

1
廣告
© . All rights reserved.