LocalDateTime计算两个时间差
时间: 2023-10-14 10:00:50 浏览: 103
要计算两个LocalDateTime之间的时间差,可以使用Duration类。下面是一个示例代码:
```java
import java.time.Duration;
import java.time.LocalDateTime;
public class Test {
public static void main(String[] args) {
LocalDateTime dateTime1 = LocalDateTime.of(2022, 1, 1, 10, 0);
LocalDateTime dateTime2 = LocalDateTime.of(2022, 1, 1, 12, 30);
Duration duration = Duration.between(dateTime1, dateTime2);
long hours = duration.toHours();
long minutes = duration.toMinutes() % 60;
System.out.println("时间差:" + hours + "小时 " + minutes + "分钟");
}
}
```
在这个例子中,我们使用`Duration.between()`方法计算了`dateTime1`和`dateTime2`之间的时间差。然后,我们使用`toHours()`方法获取小时数,<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/Maxiao1204/article/details/119038455)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文