How to compare time in Java LocalTime?
Asked Answered
U

2

6

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?

Unloosen answered 3/4, 2018 at 0:13 Comment(3)
Uh.. call equals()?Sikh
You could start by reading the JavaDocs or looking at the Date/Time TutorialsTemperature
Possible duplicate of Time comparisonUndergarment
C
3

Try:

firstLocalTime.equals(secondLocalTime);

.equals() is available for all Objects in Java.

Cornu answered 3/4, 2018 at 0:17 Comment(0)
R
10

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:

Resonant answered 3/4, 2018 at 0:17 Comment(0)
C
3

Try:

firstLocalTime.equals(secondLocalTime);

.equals() is available for all Objects in Java.

Cornu answered 3/4, 2018 at 0:17 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.