Java Duration

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java Duration class

Java Duration class is used to measures time in seconds and nanoseconds. It inherits the Object class and implements the Comparable interface.


Java Duration class declaration

Let's see the declaration of java.time.Duration class.

  1. public final class Duration extends Object   

  2. implements TemporalAmount, Comparable, Serializable  

Methods of Java Duration

Method

Description

Temporal addTo(Temporal temporal)

It is used to add this duration to the specified temporal object.

static Duration between(Temporal startInclusive, Temporal endExclusive)

It is used to obtain a Duration representing the duration between two temporal objects.

long get(TemporalUnit unit)

It is used to get the value of the requested unit.

boolean isNegative()

It is used to check if this duration is negative, excluding zero.

boolean isZero()

It is used to check if this duration is zero length.

Duration minus(Duration duration)

It is used to return a copy of this duration with the specified duration subtracted.

Duration plus(Duration duration)

It is used to return a copy of this duration with the specified duration added.

Java Duration Example: get()

  1. import java.time.*;  
  2. import java.time.temporal.ChronoUnit;  
  3. public class DurationExample {  
  4.   public static void main(String[] args) {  
  5.     Duration d = Duration.between(LocalTime.NOON,LocalTime.MAX);  
  6.     System.out.println(d.get(ChronoUnit.SECONDS));    
  7.   }  
  8. }  

Output:

43199

Java Duration Example: isNegative()

  1. import java.time.*;  
  2. public class DurationExample {  
  3.   public static void main(String[] args) {  
  4.       Duration d1 = Duration.between(LocalTime.MAX,LocalTime.NOON);  
  5.       System.out.println(d1.isNegative());  
  6.       Duration d2 = Duration.between(LocalTime.NOON,LocalTime.MAX);  
  7.               System.out.println(d2.isNegative());      
  8.   }  
  9. }  

Output:

true
false

Java Duration Example: between()

  1. import java.time.*;  
  2. import java.time.temporal.ChronoUnit;  
  3. public class DurationExample {  
  4.   public static void main(String[] args) {  
  5.     Duration d = Duration.between(LocalTime.NOON,LocalTime.MAX);  
  6.     System.out.println(d.get(ChronoUnit.SECONDS));    
  7.   }  
  8. }  

Output:

43199

Java Duration Example: minus()

  1. import java.time.*;  
  2. public class DurationExample {  
  3.   public static void main(String[] args) {  
  4.     Duration d1 = Duration.between(LocalTime.NOON,LocalTime.MAX);  
  5.     System.out.println(d1.getSeconds());  
  6.     Duration d2 = d1.minus(d1);  
  7.     System.out.println(d2.getSeconds());  
  8.   }  
  9. }  

Output:

43199
0

Java Duration Example: plus()

  1. import java.time.*;  
  2. public class DurationExample {  
  3.   public static void main(String[] args) {  
  4.     Duration d1 = Duration.between(LocalTime.NOON,LocalTime.MAX);  
  5.     System.out.println(d1.getSeconds());  
  6.     Duration d2 = d1.plus(d1);  
  7.     System.out.println(d2.getSeconds());  
  8.   }  
  9. }  

Output:

43199
86399