Duration compareTo(Duration otherDuration) example

Description

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;
//from ww w  .j a v  a 2 s  .c om
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.





















Home »
  Java Date Time »
    java.time Reference »




Clock
DayOfWeek
Duration
Instant
LocalDate
LocalDateTime
LocalTime
Month
MonthDay
OffsetDateTime
OffsetTime
Period
Year
YearMonth
ZonedDateTime
ZoneId
ZoneOffset