LocalTime equals() method in Java with Examples
The equals() method of a LocalTime class is used to compare this LocalTime object to the LocalTime passed as parameter and returns true when objects are equal else false. The comparison between both LocalTimes is based on timeline position of the local times within a day. The value to be returned by this method is determined as follows:
- if both LocalTimes are equal, then true is returned
- if both instances are not equal, then false is returned
Syntax:
public int equals(Object obj)
Parameters: This method accepts a single parameter object which is the other LocalTime to be compared, and it should not be null.
Return Value: This method returns the int value and returned value is determined as follows:
- if both LocalTimes are equal, then true is returned
- if both instances are not equal, then false is returned
Below programs illustrate the equals() method:
Program 1: When both objects are equal.
// Java program to demonstrate // LocalTime.equals() method import java.time.*; public class GFG { public static void main(String[] args) { // create a LocalTime Objects LocalTime time1 = LocalTime.parse("13:08:00"); LocalTime time2 = LocalTime.parse("13:08:00"); // print Values System.out.println("LocalTime1: " + time1); System.out.println("LocalTime2: " + time2); // compare both LocalTimes boolean value = time1.equals(time2); // print results System.out.println("Are both LocalTimes are equal: " + value); } } |
LocalTime1: 13:08 LocalTime2: 13:08 Are both LocalTimes are equal: true
Program 2: When both objects are not equal.
// Java program to demonstrate // LocalTime.equals() method import java.time.*; public class GFG { public static void main(String[] args) { // create a LocalTime Objects LocalTime time1 = LocalTime.parse("03:18:23"); LocalTime time2 = LocalTime.parse("13:08:20"); // print values System.out.println("LocalTime1: " + time1); System.out.println("LocalTime2: " + time2); // compare both LocalTimes boolean value = time1.equals(time2); // print results System.out.println("Are both LocalTimes are equal: " + value); } } |
LocalTime1: 03:18:23 LocalTime2: 13:08:20 Are both LocalTimes are equal: false
Reference: https://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#equals(java.lang.Object)
Recommended Posts:
- LocalTime plus() method in Java with Examples
- LocalTime from() method in Java with Examples
- LocalTime get() method in Java with Examples
- LocalTime until() Method in Java with Examples
- LocalTime with() Method in Java with Examples
- LocalTime ofInstant() method in Java with Examples
- LocalTime minus() method in Java with Examples
- LocalTime minusNanos() method in Java with Examples
- LocalTime minusMinutes() method in Java with Examples
- LocalTime ofNanoOfDay() method in Java with Examples
- LocalTime ofSecondOfDay() method in Java with Examples
- LocalTime atOffset() method in Java with Examples
- LocalTime minusHours() method in Java with Examples
- LocalTime query() Method in Java with Examples
- LocalTime plusNanos() 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.



