Open In App

Duration ZERO field in Java with Examples

Last Updated : 26 Nov, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads