LocalDateTime获取两个时间相差天数
时间: 2023-10-15 07:31:34 浏览: 187
要计算两个`LocalDateTime`类型时间的相差天数,可以使用`Duration`类的`between`方法进行时差计算,然后使用`toDays`方法将时差转换成天数。以下是一个示例代码:
```java
LocalDateTime start = LocalDateTime.of(2022, 5, 7, 1, 1);
LocalDateTime end = LocalDateTime.of(2022, 5, 8, 1, 1);
Duration duration = Duration.between(start, end);
long days = duration.toDays();
System.out.println("相差的天数: " + days + "天");
```
运行以上代码,将会输出:
```
相差的天数: 1天
```
这样就可以获取两个`LocalDateTime`时间的相差天数了。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [LocalDateTime两个时间相差的天数,小时数,分钟数,毫秒数](https://blog.csdn.net/zl1810311293/article/details/124635353)[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^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文