Given two LocalTime parameters in Java, I want to be able to compare the two times and see if they are equal (the same time). How would I do this?
How to compare time in Java LocalTime?
Try:
firstLocalTime.equals(secondLocalTime);
.equals()
is available for all Object
s in Java.
Compare using LocalTime
instance methods: isBefore
, isAfter
, equals
, and compareTo
.
LocalTime lt1 = LocalTime.of( 14 , 17 ) ;
LocalTime lt2 = LocalTime.of( 21 , 29 ) ;
boolean same = lt1.equals( lt2 ) ;
equals
versus isEqual
Be aware than some java.time classes (other than LocalTime
) carry an isEqual
method in addition to the equals
method inherited from Object
, with different semantics.
For example:
Try:
firstLocalTime.equals(secondLocalTime);
.equals()
is available for all Object
s in Java.
© 2022 - 2025 — McMap. All rights reserved.
equals()
? – Sikh