Duration ZERO field in Java


The ZERO field sets the duration to zero in the Duration class in Java. This field is quite the same as the null value in different data types in Java.

A program that demonstrates the ZERO field is given as follows −

Example

 Live Demo

import java.time.Duration;
public class Demo {
   public static void main(String[] args) {
      Duration d = Duration.ZERO;
      boolean flag = d.isZero();
      System.out.println("The duration is: " + d);
      if(flag)
         System.out.println("The above duration is of zero length");
      else
         System.out.println("The above duration is not of zero length");
   }
}

Output

The duration is: PT0S
The above duration is of zero length

Now let us understand the above program.

First the duration is set to zero using the ZERO field and then it is displayed. Then flag holds the returned value of isZero() method. According to the value in flag, the length of the duration is zero or not and that is printed. A code snippet that demonstrates this is as follows −

Duration d = Duration.ZERO;
boolean flag = d.isZero();
System.out.println("The duration is: " + d);
if(flag)
System.out.println("The above duration is of zero length");
else
System.out.println("The above duration is not of zero length");

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

323 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements