java Date 当前时间增加半小时
时间: 2024-05-16 16:17:47 浏览: 8
可以使用 Calendar 类来实现,具体代码如下:
```
Date now = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
calendar.add(Calendar.MINUTE, 30);
Date afterHalfAnHour = calendar.getTime();
```
首先获取当前时间的 Date 对象,然后使用 Calendar.getInstance() 获取 Calendar 对象,并将其设为当前时间。接着调用 calendar.add(Calendar.MINUTE, 30) 方法来增加半小时,最后通过 calendar.getTime() 方法获取增加半小时后的时间。
相关问题
java date 当前时间增加半小时
可以使用 Calendar 类来实现,具体代码如下:
```
Date now = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
calendar.add(Calendar.MINUTE, 30);
Date afterHalfAnHour = calendar.getTime();
```
首先获取当前时间的 Date 对象,然后使用 Calendar.getInstance() 获取 Calendar 对象,并将其设为当前时间。接着调用 calendar.add(Calendar.MINUTE, 30) 方法来增加半小时,最后通过 calendar.getTime() 方法获取增加半小时后的时间。
java date 当前时间增加8小时半
可以使用 Calendar 类来实现这个需求,具体代码如下:
```java
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR_OF_DAY, 8); // 增加8小时
calendar.add(Calendar.MINUTE, 30); // 再增加30分钟
Date date = calendar.getTime(); // 获取增加后的时间
```
另外,使用 Java 8 新增的 LocalDateTime 类也可以很方便地实现这个需求,代码如下:
```java
LocalDateTime now = LocalDateTime.now();
LocalDateTime newTime = now.plusHours(8).plusMinutes(30); // 增加8小时半
Date date = Date.from(newTime.atZone(ZoneId.systemDefault()).toInstant()); // 转换成 Date 类型
```
相关推荐
![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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)