hutool 格式化时间
时间: 2023-09-10 19:11:33 浏览: 50
使用 Hutool 库可以很方便地格式化时间,具体操作如下:
1. 引入 Hutool 依赖:
Maven:
```xml
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>x.x.x</version>
</dependency>
```
Gradle:
```groovy
implementation 'cn.hutool:hutool-all:x.x.x'
```
2. 使用 DateUtil.format 方法格式化时间,示例代码如下:
```java
import cn.hutool.core.date.DateUtil;
public class DateTest {
public static void main(String[] args) {
// 格式化当前时间
String now = DateUtil.now();
String dateStr = DateUtil.format(DateUtil.parse(now), "yyyy-MM-dd HH:mm:ss");
System.out.println(dateStr);
}
}
```
其中,`now()` 方法获取当前时间,`parse()` 方法将时间字符串转换为 `Date` 类型,`format()` 方法将 `Date` 对象格式化为指定的时间格式。以上示例代码输出的格式化时间为:`2021-11-11 11:11:11`。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)