The ZERO field of Duration Class in java.time package is used to set this duration value to zero. It is similar to null value for other data types in Java.
Syntax:
Duration duration = Duration.ZERO;
Below examples illustrate the Duration.ZERO field:
Example 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);
}
} |
Output:
PT51H4M PT0S
Example 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);
}
} |
Output:
PT-120H PT0S
Reference: https://docs.oracle.com/javase/9/docs/api/java/time/Duration.html#ZERO