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

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

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

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

public Duration minusHours(long numberOfHours)

参数:此方法接受参数numberOfHours ,它是要减去的小时数。它可以是正数或负数,但不能为空。
返回值:此方法返回一个持续时间,它是现有持续时间的不可变副本,其中减去参数小时数。
异常:如果发生数字溢出,此方法将引发ArithmeticException
下面的示例说明了 Duration.minusHours() 方法:
示例 1:

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


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


输出:
PT49H4M

示例 2:

Java

// Java code to illustrate minusHours() 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 minusHours() method
        System.out.println(duration1
                               .minusHours(5));
    }
}
输出:
PT-4H-56M

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