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



說明

java.time.YearMonth.plus(long amountToAdd, TemporalUnit unit) 方法返回添加了指定數量的本年月副本。

宣告

以下是 java.time.YearMonth.plus(long amountToAdd, TemporalUnit unit) 方法的宣告。

public YearMonth plus(long amountToAdd, TemporalUnit unit)

引數

  • amountToAdd - 要新增到結果的單位數量,可以為負數。

  • unit - 要新增的單位,非空。

返回值

基於此日期、添加了指定數量的 YearMonth,非空。

異常

  • DateTimeException - 如果不能進行此新增操作。

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

  • ArithmeticException - 如果出現數值溢位。

示例

以下示例演示了 java.time.YearMonth.plus(long amountToAdd, 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("2017-12");
      YearMonth date1 = date.plus(10, ChronoUnit.YEARS);
      System.out.println(date1);  
   }
}

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

2027-12
廣告
© . All rights reserved.