'How to compare two instants in Java?

I want comparison two Instants to see if equals both or greater than, but i can’t. i dont know. how i can compare instants?

  private Instant expiration;

 if(expiration()==Instant.now()||expiration()>Instant.now())
  {
     valid+="the Expire date is invalid check it . ";
  }

i try this way but i have compile error. but i think cant comparison in this way it must change instant to String and compare after it but i dont know how to format to string



Solution 1:[1]

Don't convert them to Strings. Instant, like every other Comparable type, has the compareTo method. Use it like this:

if (expiration.compareTo(Instant.now()) >= 0) {
  ...
}

Solution 2:[2]

Following the implementation of compareTo method of Instant class:

@Override
public int compareTo(Instant otherInstant) {
    int cmp = Long.compare(seconds, otherInstant.seconds);
    if (cmp != 0) {
        return cmp;
    }
    return nanos - otherInstant.nanos;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dunni
Solution 2 cloooze