Java 中的 Duration negated() 方法
可以在 Java 的 Duration 类中使用 negated() 方法来获取时段的不可变副本,其中时段取负。此方法不需要参数,它返回取负的时段。此外,如果发生数字溢出,将抛出 ArithmeticException。
以下给出了演示此方法的程序:
示例
import java.time.Duration;
public class GFG {
public static void main(String[] args) {
Duration d = Duration.ofHours(1);
System.out.println("The duration is: " + d);
System.out.println("A copy with negated duration is: " + d.negated());
}
}输出
The duration is: PT1H A copy with negated duration is: PT-1H
现在让我们了解一下上面的程序。
使用 negated() 方法可以获取时段的不可变副本,其中时段取负。然后显示此不可变副本。演示此方法的代码片段如下:
Duration d = Duration.ofHours(1);
System.out.println("The duration is: " + d);
System.out.println("A copy with negated duration is: " + d.negated());
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP