java 毫秒级的时间戳 并且转为能看的
时间: 2024-03-21 15:43:21 浏览: 85
好的,Java中可以使用`java.util.Date`类和`java.text.SimpleDateFormat`类将毫秒级时间戳转换为能看的日期时间格式。以下是示例代码:
```java
import java.util.Date;
import java.text.SimpleDateFormat;
public class TimestampDemo {
public static void main(String[] args) {
long timestamp = 1627480185000L; // 毫秒级时间戳
Date date = new Date(timestamp);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = sdf.format(date);
System.out.println(formattedDate);
}
}
```
以上代码将输出时间戳对应的日期时间字符串,例如:2021-07-28 16:29:45。需要注意的是,Java中的时间戳是以毫秒为单位的,因此需要在时间戳后面加上一个`L`表示长整型。
相关问题
毫秒时间戳转为日期类型java
可以使用Java的Date类或者Calendar类将毫秒时间戳转换为日期类型。
使用Date类:
```java
long timestamp = 1617863739000L; // 毫秒时间戳
Date date = new Date(timestamp);
```
使用Calendar类:
```java
long timestamp = 1617863739000L; // 毫秒时间戳
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timestamp);
Date date = calendar.getTime();
```
注意:在使用Date类时,要注意它的构造方法已经被标记为过时了,推荐使用Calendar类进行转换。
java 怎么吧时间戳转为时间
在 Java 中,可以使用 `java.util.Date` 类和 `java.text.SimpleDateFormat` 类来将时间戳转换为时间。
示例代码如下:
```java
long timestamp = 1626118900000L; // 时间戳,单位为毫秒
Date date = new Date(timestamp); // 将时间戳转为 Date 对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 定义日期格式
String time = sdf.format(date); // 将 Date 对象转为字符串
System.out.println(time); // 输出:2021-07-12 12:35:00
```
其中,`SimpleDateFormat` 构造方法中的字符串表示日期格式,具体含义如下:
- `yyyy` 表示年份,如 2021
- `MM` 表示月份,如 07
- `dd` 表示天数,如 12
- `HH` 表示小时数,24 小时制,如 12
- `mm` 表示分钟数,如 35
- `ss` 表示秒数,如 00
可以根据需要自定义日期格式。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044736.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.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)
![txt](https://img-home.csdnimg.cn/images/20241231045021.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)