Period plusYears() 方法,Java 中
可以使用 Java 中 Period 类的 plusYears() 方法获取 Period 对象的一个不可变副本,其中添加了一些年。此方法需要一个参数,即要添加的年数,它返回已添加年份的 Period 对象。
演示此功能的程序如下
示例
import java.time.Period;
public class Demo {
public static void main(String[] args) {
String period = "P5Y7M15D";
Period p1 = Period.parse(period);
System.out.println("The Period is: " + p1);
Period p2 = p1.plusYears(3);
System.out.println("The Period after adding 3 years is: " + p2);
}
}输出
The Period is: P5Y7M15D The Period after adding 3 years is: P8Y7M15D
现在让我们了解一下上述程序。
首先显示当前 Period。然后使用 plusYears() 方法获得 Period 的不可变副本,其中添加了 3 年,并显示此副本。演示此功能的代码片段如下
String period = "P5Y7M15D";
Period p1 = Period.parse(period);
System.out.println("The Period is: " + p1);
Period p2 = p1.plusYears(3);
System.out.println("The Period after adding 3 years is: " + p2);
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP