java.time.Period.plusMonths()方法

2019-10-16 22:38:56

java.time.Period.plusMonths(long months)方法返回此Period的副本,並新增指定的Period

宣告

以下是java.time.Period.plusMonths(long months)方法的宣告。

public Period plusMonths(long months)

引數

  • months - 新增的月份,正或負值。

返回值

一個基於此Period的期間,新增了指定的月份,而不是null

異常

  • ArithmeticException - 如果發生數位溢位。

範例

以下範例顯示了java.time.Period.plusMonths(long months)方法的用法。

package com.yiibai;

import java.time.Period;

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

      Period period = Period.ofMonths(2);
      Period period1 = period.plusMonths(1);
      System.out.println(period1.getMonths());   
   }
}

編譯並執行上面的程式,這將產生以下結果 -

3