How to compare time in Java LocalTime?

10,050

Solution 1

Try:

firstLocalTime.equals(secondLocalTime);

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

Solution 2

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:

Share:
10,050
Admin
Author by

Admin

Updated on June 25, 2022

Comments

  • Admin
    Admin almost 2 years

    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?