可以使用ofSeconds()
Java中Duration类中的方法以一秒的格式获得持续时间。此方法需要两个参数,即秒数和所需的以纳秒为单位的调整。同样,它以一秒的格式返回持续时间。如果超过了持续时间的容量,则抛出ArithmeticException。
演示此的程序如下所示-
import java.time.Duration; public class Demo { public static void main(String[] args) { long seconds = 515793; long adjustment = 100; Duration d1 = Duration.ofSeconds(seconds); Duration d2 = Duration.ofSeconds(seconds, adjustment); System.out.println("The duration without adjustment is: " + d1.getSeconds()); System.out.println("The duration with adjustment is: " + d2.getSeconds()); } }
输出结果
The duration without adjustment is: 515793 The duration with adjustment is: 515793
现在让我们了解上面的程序。
使用该ofSeconds()
方法以一秒钟的格式获取持续时间,然后使用和不使用调整getSeconds()
方法打印持续时间。演示这的代码片段如下-
long seconds = 515793; long adjustment = 100; Duration d1 = Duration.ofSeconds(seconds); Duration d2 = Duration.ofSeconds(seconds, adjustment); System.out.println("The duration without adjustment is: " + d1.getSeconds()); System.out.println("The duration with adjustment is: " + d2.getSeconds());