Java Instant compareTo() method

The compareTo() method of Java Instant class is used to compare this instant to the specified instant.

Syntax

Parameters

otherInstant - The other instant to compare to, not null.

Returns

The comparator value, negative if less, positive if greater.

Exceptions

NullPointerException - if otherInstant is null.

Example 1

Output:

The output will be like this.

Instant 1: 2018-07-17T10:37:30Z
Instant 2: 2018-07-17T10:37:30Z
Instant 2 is greater than Instant 1.

Example 2

Output:

1