Duration ZERO field in Java with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

PT51H4M
PT0S

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

PT-120H
PT0S

Reference: https://docs.oracle.com/javase/9/docs/api/java/time/Duration.html#ZERO



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.