java时间戳转成日期
时间: 2023-11-08 19:05:56 浏览: 37
可以使用Java中的SimpleDateFormat类来将时间戳转换为日期格式。具体代码如下:
```java
long timestamp = 162826440000L; // 时间戳,单位为毫秒
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 定义日期格式
String date = sdf.format(new Date(timestamp)); // 将时间戳转换为日期格式
System.out.println(date); // 输出:2021-08-06
```
其中,`SimpleDateFormat`类的构造方法中传入的字符串参数表示日期格式,具体含义如下:
- `yyyy`:年份,如2021
- `MM`:月份,如08
- `dd`:日期,如06
通过调用`format()`方法,将时间戳转换为指定的日期格式。最后输出的结果为`2021-08-06`。
相关问题
java 时间戳转日期
Java中时间戳转日期可以通过使用`java.util.Date`类和`java.text.SimpleDateFormat`类来实现。下面是一个示例代码:
```java
import java.util.Date;
import java.text.SimpleDateFormat;
public class TimestampToDate {
public static void main(String[] args) {
long timestamp = 1622877366000L; // 假设时间戳为 1622877366000
// 创建一个Date对象并传入时间戳
Date date = new Date(timestamp);
// 创建SimpleDateFormat对象来指定日期格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 使用SimpleDateFormat的format()方法将日期转换为字符串
String formattedDate = sdf.format(date);
System.out.println("转换后的日期为: " + formattedDate);
}
}
```
以上代码将时间戳`1622877366000`转换为格式为`yyyy-MM-dd HH:mm:ss`的日期字符串输出。你可以根据需要修改日期格式。
java时间戳转换为日期格式
可以使用 SimpleDateFormat 类将时间戳转换为日期格式。具体代码如下:
long timestamp = 162987840000L; // 时间戳,单位为毫秒
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 日期格式
String date = sdf.format(new Date(timestamp)); // 将时间戳转换为日期格式
System.out.println(date); // 输出:2021-08-25 00:00:00