Java Instant.compareTo() Method with Examples

Tags: Java Instant Java 8

In this Java core tutorial we learn how to use the java.time.Instant.compareTo() method to compare two Instant objects for ordering in Java programming language.

How to use Instant.compareTo() method

In Java, to compare two Instant objects for ordering we can use the Instant.compareTo(Instant otherInstant) method which return int value of -1, 0 or 1 if the Instant object is before, equal to or after the Instant object it compare to.

The following Java example code to show the Instant.compareTo(Instant otherInstant) method return -1 when the Instant object is before another Instant object it compare to.

InstantCompareToExample1.java

import java.time.Instant;

public class InstantCompareToExample1 {
    public static void main(String... args) {
        Instant instant1 = Instant.parse("2022-07-07T08:00:30Z");
        Instant instant2 = Instant.parse("2022-07-11T08:00:30Z");

        int result = instant1.compareTo(instant2);

        System.out.println("instant1: " + instant1);
        System.out.println("instant2: " + instant2);
        System.out.println("instant1 compare to instant2: " + result);
    }
}
The output as below.
instant1: 2022-07-07T08:00:30Z
instant2: 2022-07-11T08:00:30Z
instant1 compare to instant2: -1

The following Java example code to show the Instant.compareTo(Instant otherInstant) method return 0 when the Instant object is equals another Instant object it compare to.

InstantCompareToExample2.java

import java.time.Instant;

public class InstantCompareToExample2 {
    public static void main(String... args) {
        Instant instant1 = Instant.parse("2022-07-11T08:00:30Z");
        Instant instant2 = Instant.parse("2022-07-11T08:00:30Z");

        int result = instant1.compareTo(instant2);

        System.out.println("instant1: " + instant1);
        System.out.println("instant2: " + instant2);
        System.out.println("instant1 compare to instant2: " + result);
    }
}
The output as below.
instant1: 2022-07-11T08:00:30Z
instant2: 2022-07-11T08:00:30Z
instant1 compare to instant2: 0

The following Java example code to show the Instant.compareTo(Instant otherInstant) method return 1 when the Instant object is after another Instant object it compare to.

InstantCompareToExample3.java

import java.time.Instant;

public class InstantCompareToExample3 {
    public static void main(String... args) {
        Instant instant1 = Instant.parse("2022-07-11T08:00:30Z");
        Instant instant2 = Instant.parse("2022-07-10T08:00:30Z");

        int result = instant1.compareTo(instant2);

        System.out.println("instant1: " + instant1);
        System.out.println("instant2: " + instant2);
        System.out.println("instant1 compare to instant2: " + result);
    }
}
The output as below.
instant1: 2022-07-11T08:00:30Z
instant2: 2022-07-10T08:00:30Z
instant1 compare to instant2: 1

Happy Coding 😊

Java Instant.now() Method with Examples

Java Instant.ofEpochSecond() Method with Examples

Java Instant.ofEpochMilli() Method with Examples

Java Instant.parse() Method with Examples

Java Instant.getEpochSecond() Method with Examples

Java Instant.getNano() Method with Examples

Java Instant.plusSeconds() Method with Examples

Java Instant.plusMillis() Method with Examples

Java Instant.plusNanos() Method with Examples

Java Instant.minusSeconds() Method with Examples

Java Instant.minusMillis() Method with Examples

Java Instant.minusNanos() Method with Examples

Java Instant.atOffset() Method with Examples

Java Instant.atZone() Method with Examples

Java Instant.toEpochMilli() Method with Examples

Java Instant.isAfter() Method with Examples

Java Instant.isBefore() Method with Examples

Java Instant.equals() Method with Examples

Java Instant.toString() Method with Examples

Java Compare two Instant Values