Java Date Time - Duration compareTo(Duration otherDuration) example








Duration compareTo(Duration otherDuration) compares this duration to the specified Duration. The comparison is based on the total length of the durations.

Syntax

compareTo has the following syntax.

public int compareTo(Duration otherDuration)

Example

The following example shows how to use compareTo.

import java.time.Duration;
import java.time.LocalTime;
// w  ww  .j a v a  2  s  .c  o  m
public class Main {
  public static void main(String[] args) {
    Duration duration = Duration.between(LocalTime.MIDNIGHT,LocalTime.NOON);
    System.out.println(duration.getSeconds());
    
    System.out.println(Duration.ofSeconds(2340).compareTo(duration));    
  }
}

The code above generates the following result.