hutool工具类格式化时间戳
时间: 2023-08-23 18:08:26 浏览: 699
好的,您可以使用以下代码使用hutool工具类格式化时间戳:
```
//导入相关包
import cn.hutool.core.date.DateUtil;
//定义时间戳
long timestamp = System.currentTimeMillis();
//格式化时间戳(例如格式化成yyyy-MM-dd HH:mm:ss)
String formattedTime = DateUtil.format(new Date(timestamp), "yyyy-MM-dd HH:mm:ss");
//输出格式化后的时间戳
System.out.println(formattedTime);
```
使用hutool工具类可以方便地对时间戳进行格式化和处理。希望能对您有所帮助。
相关问题
hutool工具类时间戳转时间
以下是使用Hutool工具类进行时间戳转时间的示例代码:
```java
import cn.hutool.core.date.DateUtil;
public class Test {
public static void main(String[] args) {
long timestamp = 1626844800000L; // 时间戳,单位毫秒
String dateStr = DateUtil.date(timestamp).toString(); // 时间戳转日期字符串
System.out.println(dateStr); // 输出:2021-07-21 00:00:00
String format = "yyyy-MM-dd HH:mm:ss";
String dateStr2 = DateUtil.format(DateUtil.date(timestamp), format); // 时间戳转指定格式的日期字符串
System.out.println(dateStr2); // 输出:2021-07-21 00:00:00
long timestamp2 = DateUtil.parse(dateStr2, format).getTime(); // 日期字符串转时间戳
System.out.println(timestamp2); // 输出:1626844800000
}
}
```
hutool获取指定时间的时间戳
使用Hutool获取指定时间的时间戳,可以使用如下代码:
```java
//指定时间字符串
String timeStr = "2021-06-01 12:00:00";
//指定时间格式
String pattern = "yyyy-MM-dd HH:mm:ss";
//将时间字符串转换为指定格式的时间对象
DateTime dateTime = DateUtil.parse(timeStr, pattern);
//获取时间对象对应的时间戳(单位:毫秒)
long timestamp = dateTime.getTime();
```
其中,`timeStr`为要转换的时间字符串,`pattern`为时间字符串的格式,`dateTime`为转换后的时间对象,`timestamp`为时间对象对应的时间戳(单位:毫秒)。
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)