Java中的 Duration minusNanos(long) 方法和示例
Java.time 包中Duration 类的minusNanos(long)方法用于获取此持续时间的不可变副本,减去指定的纳秒数,作为参数传递。
句法:
public Duration minusNanos(long numberOfNanos)
参数:此方法接受参数numberOfNanos ,它是要减去的纳秒数。它可以是正数或负数,但不能为空。
返回值:此方法返回一个持续时间,它是现有持续时间的不可变副本,其中减去了纳秒的参数量。
异常:如果发生数字溢出,此方法将引发ArithmeticException 。
下面的示例说明了 Duration.minusNanos() 方法:
示例 1:
Java
// Java code to illustrate minusNanos() method
import java.time.Duration;
public class GFG {
public static void main(String[] args)
{
// Duration 1 using parse() method
Duration duration1
= Duration.parse("P2DT3H4M");
// Get the duration subtracted
// using minusNanos() method
System.out.println(duration1.minusNanos(2));
}
}
Java
// Java code to illustrate minusNanos() method
import java.time.Duration;
public class GFG {
public static void main(String[] args)
{
// Duration 1 using parse() method
Duration duration1
= Duration.parse("P0DT0H4M");
// Get the duration subtracted
// using minusNanos() method
System.out.println(duration1.minusNanos(5));
}
}
输出:
PT51H3M59.999999998S
示例 2:
Java
// Java code to illustrate minusNanos() method
import java.time.Duration;
public class GFG {
public static void main(String[] args)
{
// Duration 1 using parse() method
Duration duration1
= Duration.parse("P0DT0H4M");
// Get the duration subtracted
// using minusNanos() method
System.out.println(duration1.minusNanos(5));
}
}
输出:
PT3M59.999999995S
参考: https://docs.oracle.com/javase/9/docs/api/ Java/time/Duration.html#minusNanos-long-