Period plusDays()方法在Java中
可以使用Java 中 Period 类的 plusDays() 方法获取 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.plusDays(5);
System.out.println("The Period after adding 5 days is: " + p2);
}
}输出
The Period is: P5Y7M15D The Period after adding 5 days is: P5Y7M20D
让我们了解一下上面的程序。
首先显示当前时间段。然后,使用 plusDays() 方法获得 Period 的不可变副本,其中添加了 5 天,并显示该副本。一个演示此代码片段如下
String period = "P5Y7M15D";
Period p1 = Period.parse(period);
System.out.println("The Period is: " + p1);
Period p2 = p1.plusDays(5);
System.out.println("The Period after adding 5 days is: " + p2);
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C编程
C++
C#
MongoDB
MySQL
Javascript
PHP