在Java中,可以使用java.util.Datejava.time.LocalDateTime类来将13位时间戳转换为日期。以下是两种方法的示例代码:

  1. 使用java.util.Date类:
long timestamp = 1596498034000L; // 13位时间戳

Date date = new Date(timestamp);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = sdf.format(date);

System.out.println(formattedDate);
  1. 使用java.time.LocalDateTime类(Java 8及以上版本):
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

long timestamp = 1596498034000L; // 13位时间戳

Instant instant = Instant.ofEpochMilli(timestamp);
LocalDateTime date = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDate = date.format(formatter);

System.out.println(formattedDate);

无论使用哪种方法,输出结果都将是形如yyyy-MM-dd HH:mm:ss的日期字符串

java 13位时间戳转日期

原文地址: https://www.cveoy.top/t/topic/ipVt 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录