localdatetime 比较大小
时间: 2023-09-23 11:07:07 浏览: 101
LocalDateTime类实现了Comparable接口,因此可以使用compareTo()方法比较两个LocalDateTime对象的大小。如果第一个LocalDateTime对象早于第二个,则返回负数;如果第一个LocalDateTime对象晚于第二个,则返回正数;如果两个LocalDateTime对象相等,则返回0。以下是比较两个LocalDateTime对象大小的示例代码:
```
LocalDateTime dateTime1 = LocalDateTime.of(2021, 10, 1, 10, 30);
LocalDateTime dateTime2 = LocalDateTime.of(2021, 10, 1, 11, 0);
int result = dateTime1.compareTo(dateTime2);
if (result < 0) {
System.out.println(dateTime1 + " is earlier than " + dateTime2);
} else if (result > 0) {
System.out.println(dateTime1 + " is later than " + dateTime2);
} else {
System.out.println(dateTime1 + " is equal to " + dateTime2);
}
```
输出结果为:
```
2021-10-01T10:30 is earlier than 2021-10-01T11:00
```
阅读全文