java比较两个时间大小
时间: 2023-11-07 22:02:53 浏览: 68
可以使用Java中的Date类或者LocalDateTime类来比较两个时间的大小。具体实现可以使用compareTo()方法或isAfter()/isBefore()方法。以下是一个示例代码:
```java
import java.time.LocalDateTime;
public class CompareTime {
public static void main(String[] args) {
LocalDateTime time1 = LocalDateTime.of(2021, 5, 1, 10, 30);
LocalDateTime time2 = LocalDateTime.of(2021, 5, 2, 9, 0);
int result = time1.compareTo(time2);
if (result < 0) {
System.out.println("time1 is earlier than time2");
} else if (result > 0) {
System.out.println("time1 is later than time2");
} else {
System.out.println("time1 and time2 are equal");
}
}
}
```
输出结果为:"time1 is earlier than time2"。
阅读全文