简体   繁体   中英

Java LocalTime how to compare time?

Given two LocalTime parameters in Java, I want to be able to compare the two times are see if they are equal (same time). How would I do this?

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 ) ;

Be aware than some java.time classes (other than LocalTime ) carry an isEqual method in addition to equals , with different semantics. For example:

Try:

firstLocalTime.equals(secondLocalTime);

.equals() is available for all Object s in Java.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM