简体   繁体   English

Java LocalTime如何比较时间?

[英]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). 给定Java中的两个LocalTime参数,我希望能够比较两个时间,看看它们是否相等(相同时间)。 How would I do this? 我该怎么做?

Compare using LocalTime instance methods: isBefore , isAfter , equals , and compareTo . 通过比较LocalTime实例方法: isBeforeisAfterequalscompareTo

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. 要知道比一些java.time类(除LocalTime )携带isEqual除了法equals ,具有不同的语义。 For example: 例如:

Try: 尝试:

firstLocalTime.equals(secondLocalTime);

.equals() is available for all Object s in Java. .equals()可用于Java中的所有Object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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