Java中的 Duration minusDays(long) 方法和示例
Java.time 包中Duration 类的minusDays(long)方法用于获取此持续时间的不可变副本,其中减去指定的天数,作为参数传递。
句法:
public Duration minusDays(long numberOfDays)
参数:此方法接受参数numberOfDays ,它是要减去的天数。它可以是正数或负数,但不能为空。
返回值:此方法返回一个持续时间,它是现有持续时间的不可变副本,其中减去参数天数。
异常:如果发生数字溢出,此方法将引发ArithmeticException 。
下面的示例说明了 Duration.minusDays() 方法:
示例 1:
Java
// Java code to illustrate minusDays() 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 minusDays() method
System.out.println(duration1.minusDays(2));
}
}
Java
// Java code to illustrate minusDays() 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 minusDays() method
System.out.println(duration1.minusDays(5));
}
}
输出:
PT3H4M
示例 2:
Java
// Java code to illustrate minusDays() 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 minusDays() method
System.out.println(duration1.minusDays(5));
}
}
输出:
PT-119H-56M
参考: https://docs.oracle.com/javase/9/docs/api/ Java/time/Duration.html#minusDays-long-