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



说明

java.time.Period.withYears(int years) 方法返回带有指定年份的此周期的副本。

声明

以下是 java.time.Period.withYears(int years) 方法的声明。

public Period withYears(int years)

public Period withYears(int years)

参数

years − 表示年份,可以为负数。

返回值

基于此周期的 Period,使用请求的年份,非空。

示例

package com.tutorialspoint;

import java.time.Period;

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

      Period period = Period.ofYears(2);
      System.out.println(period.toString());  
      period = period.withYears(5);
      System.out.println(period.toString());  
   }
}

实时演示

P2Y
P5Y
打印页面
© . All rights reserved.