Java Compare two Instant Values

Tags: Java Instant Java 8

In this Java core tutorial we learn how to compare two java.time.Instant. objects in Java programming language using compareTo(), equals(), isBefore() and isAfter() methods.

Table of contents

  1. Compare two Instant objects for Ordering
  2. Compare two Instant objects for Equality
  3. Compare if an Instant object is before another Instant object
  4. Compare if an Instant object is after another Instant object

Compare two Instant objects for Ordering

In Java, to compare two Instant objects for ordering we can use the Instant.compareTo(Instant otherInstant) method which return integer 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 program, we show the Instant.compareTo(Instant otherInstant) method return -1 when the Instant object is before another Instant object it compare to.

CompareInstantExample1.java

import java.time.Instant;

public class CompareInstantExample1 {
    public static void main(String... args) {
        Instant instant1 = Instant.parse("2022-05-10T07:30:30Z");
        Instant instant2 = Instant.parse("2022-05-16T07:30: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-05-10T07:30:30Z
instant2: 2022-05-16T07:30:30Z
instant1 compare to instant2: -1

The following Java program, we show the Instant.compareTo(Instant otherInstant) method return 0 when the Instant object is equal another Instant object it compare to.

CompareInstantExample2.java

import java.time.Instant;

public class CompareInstantExample2 {
    public static void main(String... args) {
        Instant instant1 = Instant.parse("2022-05-16T07:30:30Z");
        Instant instant2 = Instant.parse("2022-05-16T07:30: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-05-16T07:30:30Z
instant2: 2022-05-16T07:30:30Z
instant1 compare to instant2: 0

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

CompareInstantExample3.java

import java.time.Instant;

public class CompareInstantExample3 {
    public static void main(String... args) {
        Instant instant1 = Instant.parse("2022-05-20T07:30:30Z");
        Instant instant2 = Instant.parse("2022-05-16T07:30: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-05-20T07:30:30Z
instant2: 2022-05-16T07:30:30Z
instant1 compare to instant2: 1

Compare two Instant objects for Equality

In Java to compare two Instant values are equal or not we can use the Instant.equals(Object otherInstant) method as the following example Java code.

CompareInstantExample4.java

import java.time.Instant;

public class CompareInstantExample4 {
    public static void main(String... args) {
        Instant instant1 = Instant.parse("2022-05-16T07:30:30Z");
        Instant instant2 = Instant.parse("2022-05-16T07:30:30Z");
        Instant instant3 = Instant.parse("2022-06-20T07:30:30Z");

        boolean result1 = instant1.equals(instant2);
        boolean result2 = instant1.equals(instant3);

        System.out.println("instant1: " + instant1);
        System.out.println("instant2: " + instant2);
        System.out.println("instant3: " + instant3);
        System.out.println("instant1 is equals instant2: " + result1);
        System.out.println("instant1 is equals instant3: " + result2);
    }
}
The output as below.
instant1: 2022-05-16T07:30:30Z
instant2: 2022-05-16T07:30:30Z
instant3: 2022-06-20T07:30:30Z
instant1 is equals instant2: true
instant1 is equals instant3: false

Compare if an Instant object is before another Instant object

In Java to check if an Instant object is before another specified Instant object we can use the Instant.isBefore(Instant otherInstant) method as following Java code.

CompareInstantExample5.java

import java.time.Instant;

public class CompareInstantExample5 {
    public static void main(String... args) {
        Instant instant1 = Instant.parse("2022-05-16T07:30:30Z");
        Instant instant2 = Instant.parse("2022-05-16T07:30:30Z");
        Instant instant3 = Instant.parse("2022-06-20T07:30:30Z");

        boolean result1 = instant1.isBefore(instant2);
        boolean result2 = instant1.isBefore(instant3);

        System.out.println("instant1: " + instant1);
        System.out.println("instant2: " + instant2);
        System.out.println("instant3: " + instant3);
        System.out.println("instant1 is before instant2: " + result1);
        System.out.println("instant1 is before instant3: " + result2);
    }
}
The output as below.
instant1: 2022-05-16T07:30:30Z
instant2: 2022-05-16T07:30:30Z
instant3: 2022-06-20T07:30:30Z
instant1 is before instant2: false
instant1 is before instant3: true

Compare if an Instant object is after another Instant object

In Java to check if an Instant object is after another specified Instant object we can use the Instant.isAfter(Instant otherInstant) method as following Java code.

CompareInstantExample6.java

import java.time.Instant;

public class CompareInstantExample6 {
    public static void main(String... args) {
        Instant instant1 = Instant.parse("2022-05-20T07:30:30Z");
        Instant instant2 = Instant.parse("2022-05-16T07:30:30Z");
        Instant instant3 = Instant.parse("2022-06-20T07:30:30Z");

        boolean result1 = instant1.isAfter(instant2);
        boolean result2 = instant1.isAfter(instant3);

        System.out.println("instant1: " + instant1);
        System.out.println("instant2: " + instant2);
        System.out.println("instant3: " + instant3);
        System.out.println("instant1 is after instant2: " + result1);
        System.out.println("instant1 is after instant3: " + result2);
    }
}
The output as below.
instant1: 2022-05-20T07:30:30Z
instant2: 2022-05-16T07:30:30Z
instant3: 2022-06-20T07:30:30Z
instant1 is after instant2: true
instant1 is after instant3: false

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.compareTo() 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