Java LocalDateTime 日期比较
时间: 2023-08-16 11:14:12 浏览: 112
Java LocalDateTime 日期比较可以使用compareTo()方法。该方法用于将两个LocalDateTime对象进行比较。如果第一个对象在时间上早于第二个对象,则返回负数;如果第一个对象在时间上晚于第二个对象,则返回正数;如果两个对象在时间上相等,则返回0。以下是一个示例代码:
```java
LocalDateTime time1 = LocalDateTime.now();
LocalDateTime time2 = LocalDateTime.of(2021, 9, 17, 16, 33, 12);
int comparison = time1.compareTo(time2);
if (comparison < 0) {
System.out.println("time1在time2之前");
} else if (comparison > 0) {
System.out.println("time1在time2之后");
} else {
System.out.println("time1和time2相等");
}
```
在这个示例中,我们首先创建了两个LocalDateTime对象:time1表示当前时间,time2表示指定的时间。然后使用compareTo()方法比较这两个对象,并根据比较结果输出相应的信息。
#### 引用[.reference_title]
- *1* [java获取当前时间的方法:LocalDateTime、Date、Calendar,以及三者的比较](https://blog.csdn.net/sunzixiao/article/details/129166030)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [JAVA 日期时间比较](https://blog.csdn.net/xnn_fjj/article/details/120352665)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [Java 日期转换、比较](https://blog.csdn.net/weixin_44675513/article/details/112348341)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文