hutool 格式化时间
时间: 2023-07-28 08:31:04 浏览: 126
使用 Hutool 格式化时间需要使用 `DateUtil` 工具类,示例代码如下:
```java
import cn.hutool.core.date.DateUtil;
public class Test {
public static void main(String[] args) {
// 获取当前时间
long currentTimeMillis = System.currentTimeMillis();
// 格式化时间
String format1 = DateUtil.format(new Date(currentTimeMillis), "yyyy-MM-dd HH:mm:ss");
String format2 = DateUtil.format(new Date(currentTimeMillis), "yyyy/MM/dd HH:mm:ss");
System.out.println(format1);
System.out.println(format2);
}
}
```
输出结果:
```
2022-01-14 11:22:33
2022/01/14 11:22:33
```
其中,第一个参数是需要格式化的时间,第二个参数是格式化的模板。`DateUtil` 还提供了很多其他的时间格式化方法,可以根据具体需求使用。
相关问题
hutool格式化时间为年月日时分
使用Hutool进行时间格式化可以使用DateTime类的format方法。
示例代码如下:
```java
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
public class Test {
public static void main(String[] args) {
DateTime now = DateUtil.date();
String format = DateUtil.format(now, "yyyy-MM-dd HH:mm");
System.out.println(format);
}
}
```
输出结果为:2022-02-22 10:30
hutool 时间格式化类
Hutool是Java语言的工具库,其中也包含了时间格式化类。时间格式化类的主要作用是将Java中的Date对象或者long类型的时间戳转换为指定的日期格式的字符串。
Hutool中的时间格式化类是DateUtil,它提供了许多静态方法,可以方便地进行时间格式化操作。以下是一些常用的方法:
1. date():获取当前时间的Date对象。
2. parse():将字符串转换为Date对象。
3. format():将Date对象转换为指定格式的字符串。
4. formatDateTime():将Date对象转换为yyyy-MM-dd HH:mm:ss格式的字符串。
5. formatDate():将Date对象转换为yyyy-MM-dd格式的字符串。
6. formatTime():将Date对象转换为HH:mm:ss格式的字符串。
例如,将当前时间格式化为yyyy-MM-dd HH:mm:ss格式的字符串,可以使用以下代码:
```
String nowStr = DateUtil.formatDateTime(new Date());
```
需要注意的是,Hutool中的时间格式化类默认使用系统的时区进行时间转换,如果需要使用其他时区,可以使用TimeZone类进行设置。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)