Java中的持续时间零字段与示例
Java.time 包中的Duration 类的ZERO字段用于将此持续时间值设置为零。它类似于Java中其他数据类型的 null 值。
句法:
Duration duration = Duration.ZERO;
下面的示例说明了 Duration.ZERO 字段:
示例 1:
// Java code to illustrate ZERO field
import java.time.Duration;
public class GFG {
public static void main(String[] args)
{
// Duration using parse() field
Duration duration
= Duration.parse("P2DT3H4M");
// Display the duration
System.out.println(duration);
// Now set the duration to ZERO
duration = Duration.ZERO;
// Display the duration
System.out.println(duration);
}
}
输出:
PT51H4M
PT0S
示例 2:
// Java code to illustrate ZERO field
import java.time.Duration;
public class GFG {
public static void main(String[] args)
{
// Duration
Duration duration
= Duration.ofDays(-5);
// Display the duration
System.out.println(duration);
// Now set the duration to ZERO
duration = Duration.ZERO;
// Display the duration
System.out.println(duration);
}
}
输出:
PT-120H
PT0S
参考: https: Java/docs/api/java/time/Duration.html#ZERO