Java中的持续时间abs()方法

可以使用abs()Java中Duration类中的方法获得持续时间的不变副本。此方法不需要任何参数,它返回持续时间的不变副本。另外,如果发生数字溢出,则会引发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 of the above duration is: " + d.abs());
   }
}

输出结果

The duration is: PT1H
A copy of the above duration is: PT1H

现在让我们了解上面的程序。

使用该abs()方法可以获得持续时间的不变副本。然后显示。演示这的代码片段如下-

Duration d = Duration.ofHours(1);
System.out.println("The duration is: " + d);
System.out.println("A copy of the above duration is: " + d.abs());