當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java 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)

參數

  • endDateExclusive− 結束日期,獨占,轉換為 YearMonth,不為 null。

  • unit− 計量數量的單位,不為空。

返回值

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

異常

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

  • UnsupportedTemporalTypeException− 如果不支持該單元。

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

示例

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

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

相關用法


注:本文由純淨天空篩選整理自 java.time.YearMonth.until() Method Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。