Java Instant.equals() Method with Examples

Tags: Java Instant Java 8

In this Java core tutorial we learn how to use the java.time.Instant.equals() method to check if an Instant object is equals another Instant object or not in Java programming language.

How to use Instant.equals() method

In Java, to check if an Instant object is equals another Instant object or not we can use the Instant.equals(Object otherInstant) method as Java code below.

InstantEqualsExample1.java

import java.time.Instant;

public class InstantEqualsExample1 {
    public static void main(String... args) {
        Instant instant1 = Instant.parse("2022-07-11T08:00:30Z");
        Instant instant2 = Instant.parse("2022-07-11T08:00:30Z");
        Instant instant3 = Instant.parse("2022-07-15T08:00: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-07-11T08:00:30Z
instant2: 2022-07-11T08:00:30Z
instant3: 2022-07-15T08:00:30Z
instant1 is equals instant2: true
instant1 is equals 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.toString() Method with Examples

Java Compare two Instant Values