The equals(Object otherInstant) method of Instant class is used to compare this Instant to the Instant object passed as parameter. The comparison between both instances is based on the time-line position of the instants. The value to be returned by this method is determined as follows:
- if both instances are equal, then true is returned
- if both instances are not equal, then false is returned.
Syntax:
public boolean equals(Object otherInstant)
Parameters: This method accepts a mandatory parameter otherInstant which is the other instant to be compared, and it should not be null.
Return Value: This method returns a boolean value as follows:
- true: if both instances are equal.
- false: if both instances are not equal.
Below programs illustrate the Instant.equals() method:
Program 1: When both instances are equal
// Java program to demonstrate // Instant.equals() method import java.time.*; public class GFG { public static void main(String[] args) { // create two instance objects Instant instant1 = Instant.parse("2018-10-20T16:55:30.00Z"); Instant instant2 = Instant.parse("2018-10-20T16:55:30.00Z"); // print Instant Values System.out.println("Instant1: " + instant1); System.out.println("Instant2: " + instant2); // compare both instant boolean value = instant1.equals(instant2); // print results System.out.println("Are both instants are equal: " + value); } } |
Instant1: 2018-10-20T16:55:30Z Instant2: 2018-10-20T16:55:30Z Are both instants are equal: true
Program 2: When both instances are not equal
// Java program to demonstrate // Instant.equals() method import java.time.*; public class GFG { public static void main(String[] args) { // create two instance objects Instant instant1 = Instant.parse("2018-10-20T16:55:30.00Z"); Instant instant2 = Instant.parse("2011-10-20T16:55:30.00Z"); // print Instant Values System.out.println("Instant1: " + instant1); System.out.println("Instant2: " + instant2); // compare both instant boolean value = instant1.equals(instant2); // print results System.out.println("Are both instants are equal: " + value); } } |
Instant1: 2018-10-20T16:55:30Z Instant2: 2011-10-20T16:55:30Z Are both instants are equal: false
Reference: https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#equals(java.lang.Object)
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Instant isSupported() method in Java with Examples
- Java 8 Clock instant() method with Examples
- Instant compareTo() method in Java with Examples
- Instant atZone() method in Java with Examples
- Instant atOffset() method in Java with Examples
- Instant getEpochSecond() method in Java with Examples
- Instant getLong() method in Java with Examples
- Instant getNano() method in Java with Examples
- Instant hashCode() method in Java with Examples
- Instant toEpochMilli() method in Java with Examples
- Instant isAfter() method in Java with Examples
- Instant isBefore() method in Java with Examples
- Instant from() method in Java with Examples
- Instant get() method in Java with Examples
- Instant minusMillis() method in Java with Examples
- Instant minusSeconds() method in Java with Examples
- Instant minusNanos() method in Java with Examples
- Instant plusNanos() method in Java with Examples
- Instant plusMillis() method in Java with Examples
- Instant range() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Kirti_Mangal

