时间戳 毫秒换成时间
时间: 2023-11-14 08:08:51 浏览: 50
可以使用 Python 中的 datetime 模块来将时间戳转换为时间。具体实现代码如下:
```python
import datetime
timestamp = 1629878400000 # 时间戳,单位为毫秒
dt = datetime.datetime.fromtimestamp(timestamp / 1000) # 将毫秒转换为秒
print(dt.strftime('%Y-%m-%d %H:%M:%S')) # 输出格式化后的时间字符串
```
输出结果为:2021-08-25 00:00:00
相关问题
java时间戳转换成时间
您可以使用Java的`java.util.Date`类和`java.text.SimpleDateFormat`类来将时间戳转换为时间。下面是一个示例代码:
```java
import java.util.Date;
import java.text.SimpleDateFormat;
public class TimestampConverter {
public static void main(String[] args) {
long timestamp = 1629144079000L; // 时间戳,单位为毫秒
// 创建SimpleDateFormat对象,定义时间格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 将时间戳转换为Date对象
Date date = new Date(timestamp);
// 使用SimpleDateFormat对象的format方法将Date对象转换为字符串
String formattedDate = sdf.format(date);
System.out.println("时间戳:" + timestamp);
System.out.println("转换后的时间:" + formattedDate);
}
}
```
运行以上代码,输出结果如下:
```
时间戳:1629144079000
转换后的时间:2021-08-17 15:34:39
```
在以上示例中,我们首先创建了一个`SimpleDateFormat`对象`sdf`,定义了要转换的时间格式为"yyyy-MM-dd HH:mm:ss"。然后,使用`Date`类的构造函数将时间戳转换为`Date`对象。最后,使用`sdf`对象的`format`方法将`Date`对象格式化为字符串表示的时间。
将时间转换成毫秒级时间戳
将时间转换成毫秒级时间戳,可以使用以下 Python 代码:
```python
import time
# 获取当前时间的毫秒级时间戳
millis = int(round(time.time() * 1000))
print(millis)
```
该代码首先使用 `time.time()` 函数获取当前时间的秒级时间戳,然后将其乘以 1000 并四舍五入,得到毫秒级时间戳,最后使用 `int()` 函数将其转换为整数类型。运行该代码,即可输出当前时间的毫秒级时间戳。
相关推荐
![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_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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)