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.