Java 根据开始时间以及间隔时间计算出结束时间实现
时间: 2024-05-15 16:12:43 浏览: 7
可以使用Java中的`LocalDateTime`类和`Duration`类来实现。
假设有以下参数:
- `start`:开始时间,类型为`LocalDateTime`
- `interval`:间隔时间,类型为`Duration`
则可以使用以下代码计算出结束时间:
```java
LocalDateTime end = start.plus(interval);
```
完整的示例代码如下:
```java
import java.time.Duration;
import java.time.LocalDateTime;
public class Main {
public static void main(String[] args) {
LocalDateTime start = LocalDateTime.now();
Duration interval = Duration.ofMinutes(30);
LocalDateTime end = start.plus(interval);
System.out.println("Start time: " + start);
System.out.println("End time: " + end);
}
}
```
输出结果为:
```
Start time: 2021-10-28T14:30:00.123456
End time: 2021-10-28T15:00:00.123456
```
其中`Duration.ofMinutes(30)`表示间隔时间为30分钟,可以根据需要修改。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)