持续时间minusNanos()方法在Java中

可以使用Java的Duration类中的minusNanos()方法来获取持续时间的不变副本,其中从其中删除了几纳秒的时间。此方法需要一个参数,即要减去的纳秒数,并返回减去的纳秒数的持续时间。

演示此的程序如下所示-

示例

import java.time.Duration;
public class Demo {
   public static void main(String[] args) {
      Duration d = Duration.ofSeconds(1);
      System.out.println("The duration is: " + d);
      System.out.println("A copy with 100 nano seconds removed from the duration is: " + d.minusNanos(100));
   }
}

输出结果

The duration is: PT1S
A copy with 100 nano seconds removed from the duration is: PT0.9999999S

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

首先,显示持续时间。然后,使用minusNanos()方法获得删除100纳秒的持续时间的不变副本,并将其显示出来。演示这的代码片段如下-

Duration d = Duration.ofSeconds(1);
System.out.println("The duration is: " + d);
System.out.println("A copy with 100 nano seconds removed from the duration is: " + d.minusNanos(100));