Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare time in Java LocalTime?

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?

like image 375
FallonXay Avatar asked Jul 19 '26 01:07

FallonXay


1 Answers

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:

  • LocalDate.isEqual versus LocalDate.equals
  • ZonedDateTime.isEqual versus ZonedDateTime.equals
like image 105
Basil Bourque Avatar answered Jul 22 '26 16:07

Basil Bourque



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!