Duration 类中的 minusDays() 方法
在 Java 的 Duration 类中使用 minusDays() 方法,可以获取一个不可变的持续时间副本,并从中减去一些天数。此方法需要一个参数,即要减去的天数,返回已减去天数的持续时间。
演示此方法的程序如下 –
示例
import java.time.Duration;
public class Demo {
public static void main(String[] args) {
Duration d = Duration.ofDays(5);
System.out.println("The duration is: " + d);
System.out.println("A copy with 2 days removed from the duration is: " + d.minusDays(2));
}
}输出
The duration is: PT120H A copy with 2 days removed from the duration is: PT72H
下面看一如何理解以上的代码。
首先,显示持续时间。然后使用 minusDays() 方法获取持续时间的不可变副本,该副本移除了 2 天,然后显示该副本。演示此操作的代码片段如下 –
Duration d = Duration.ofDays(5);
System.out.println("The duration is: " + d);
System.out.println("A copy with 2 days removed from the duration is: " + d.minusDays(2));
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP