- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Instant equals() method in Java
The equality of two Instant objects can be determined using the equals() method in the Instant class in Java. This method requires a single parameter i.e. the Instant to be compared. Also it returns true if both the Instant objects are equal and false otherwise.
A program that demonstrates this is given as follows
Example
import java.time.*; import java.time.temporal.ChronoUnit; public class Demo { public static void main(String[] args) { Instant i1 = Instant.parse("2019-01-13T16:10:35.00Z"); Instant i2 = Instant.parse("2019-01-13T16:10:35.00Z"); System.out.println("Instant object i1 is: " + i1); System.out.println("Instant object i2 is: " + i2); boolean flag = i1.equals(i2); if(flag) System.out.println("
The Instant objects are equal"); else System.out.println("
The Instant objects are not equal"); } }
Output
Instant object i1 is: 2019-01-13T16:10:35Z Instant object i2 is: 2019-01-13T16:10:35Z The Instant objects are equal
Now let us understand the above program.
The two instant objects i1 and i2 are displayed. It is checked if the Instant objects are equal using the equals() method. The returned value of the method is displayed using an if statement. A code snippet that demonstrates this is as follows:
Instant i1 = Instant.parse("2019-01-13T16:10:35.00Z"); Instant i2 = Instant.parse("2019-01-13T16:10:35.00Z"); System.out.println("Instant object i1 is: " + i1); System.out.println("Instant object i2 is: " + i2); boolean flag = i1.equals(i2); if(flag) System.out.println("
The Instant objects are equal"); else System.out.println("
The Instant objects are not equal");
Advertisements