Java Date Time - Instant compareTo(Instant otherInstant) example








Instant compareTo(Instant otherInstant) compares this instant to the specified instant. The comparison is based on the time-line position of the instants.

Syntax

compareTo has the following syntax.

public int compareTo(Instant otherInstant)

Example

The following example shows how to use compareTo.

import java.time.Instant;

public class Main {
  public static void main(String[] args) {
    Instant instant = Instant.parse("2014-12-03T10:15:30.00Z");
    System.out.println(instant.compareTo(Instant.now()));
 
  }
}

The code above generates the following result.