java.time.Period.withMonths() 方法示例



說明

java.time.Period.withMonths(int months) 方法返回此 Period 一個具有指定月份的副本。

宣告

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

public Period withMonths(int months)

引數

months − 要表示的月份,可能為負。

返回值

基於此週期具有所請求的月的新週期,不為 null。

示例

以下示例演示了 java.time.Period.withMonths(int months) 方法的使用。

package com.tutorialspoint;

import java.time.Period;

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

      Period period = Period.ofMonths(2);
      System.out.println(period.toString());  
      period = period.withMonths(5);
      System.out.println(period.toString());  
   }
}

讓我們編譯並執行以上程式,結果如下 −

P2M
P5M
廣告
© . All rights reserved.