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



描述

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

宣告

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

public long until(Temporal endExclusive, TemporalUnit unit)

``` **public long until(Temporal endExclusive, TemporalUnit unit)**```

  • 引數

  • endDateExclusive - 結束日期(不包括),轉換為 YearMonth,不能為空。

unit - 要用於計量的時間單位,不能為空。

返回值

此日期與結束日期之間的時長。

  • 異常

  • DateTimeException - 無法計算時間量,或者無法將最終時間轉換為 YearMonth。

  • UnsupportedTemporalTypeException - 不支援該單位。

ArithmeticException - 出現數字溢位。

示例

package com.tutorialspoint;

import java.time.YearMonth;
import java.time.temporal.ChronoUnit;

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

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

即時演示

2
列印頁面
© . All rights reserved.