📌  相关文章
📜  Java中的 Duration minusMillis(long) 方法和示例

📅  最后修改于: 2022-05-13 01:55:20.416000             🧑  作者: Mango

Java中的 Duration minusMillis(long) 方法和示例

Java.time 包Duration 类minusMillis(long)方法用于获取此持续时间的不可变副本,减去指定的毫秒数,作为参数传递。

句法:

public Duration minusMillis(long numberOfMillis)

参数:此方法接受参数numberOfMillis ,它是要减去的毫秒数。它可以是正数或负数,但不能为空。

返回值:此方法返回一个持续时间,它是现有持续时间的不可变副本,其中减去了毫秒的参数量。

异常:如果发生数字溢出,此方法将引发ArithmeticException

下面的示例说明了 Duration.minusMillis() 方法:

示例 1:

Java
// Java code to illustrate minusMillis() 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 minusMillis() method
        System.out.println(duration1.minusMillis(2));
    }
}


Java
// Java code to illustrate minusMillis() 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 minusMillis() method
        System.out.println(duration1.minusMillis(5));
    }
}


输出:
PT51H3M59.998S

示例 2:

Java

// Java code to illustrate minusMillis() 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 minusMillis() method
        System.out.println(duration1.minusMillis(5));
    }
}
输出:
PT3M59.995S

参考: https://docs.oracle.com/javase/9/docs/api/ Java/time/Duration.html#minusMillis-long-