Skip to content
Related Articles

Related Articles

Duration ZERO field in Java with Examples
  • Last Updated : 26 Nov, 2018

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :