在Java中,时间戳可以通过java.util.Date
类或java.time.Instant
类来转换为日期。
使用java.util.Date
类的示例如下:
long timestamp = 1610467200000L; // 时间戳,单位为毫秒
Date date = new Date(timestamp);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = sdf.format(date);
System.out.println(formattedDate);
使用java.time.Instant
类的示例如下:
long timestamp = 1610467200000L; // 时间戳,单位为毫秒
Instant instant = Instant.ofEpochMilli(timestamp);
LocalDateTime dateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDate = dateTime.format(formatter);
System.out.println(formattedDate);
以上代码将时间戳转换为日期,并格式化输出。您可以根据需要选择使用java.util.Date
类或java.time.Instant
类。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:java毫秒时间戳如何转换为日期