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



描述

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

宣告

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

public long until(Temporal endExclusive, TemporalUnit unit)

引數

  • endDateExclusive − 結束日期(不包括),轉換為 LocalDate,非空。

  • unit − 測量數量的單位,非空。

返回值

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

異常

  • DateTimeException − 如果無法計算數量,或者無法將結束時間轉換為 LocalDate。

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

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

示例

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

package com.tutorialspoint;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class LocalDateDemo {
   public static void main(String[] args) {
	  LocalDate date = LocalDate.parse("2017-01-03");
      LocalDate date1 = LocalDate.now();
      System.out.println(date.until(date1, ChronoUnit.DAYS));  
   }
}

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

343
廣告
© . All rights reserved.