java 时间戳字符串转localdatetime
要将时间戳字符串转换为LocalDateTime对象,你可以使用java.time包中的Instant类和LocalDateTime类。
以下是一个示例代码:
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class Main {
public static void main(String[] args) {
String timestampString = "1577836800000"; // 时间戳字符串
// 将时间戳字符串转换为Instant对象
Instant instant = Instant.ofEpochMilli(Long.parseLong(timestampString));
// 将Instant对象转换为LocalDateTime对象
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
System.out.println(localDateTime);
}
}
在上面的代码中,我们首先将时间戳字符串转换为Instant对象,然后使用ofInstant()方法将Instant对象转换为LocalDateTime对象。在ofInstant()方法中,我们需要指定一个ZoneId对象,用于将Instant对象转换为本地时间。在上面的示例中,我们使用了系统默认的时区。
输出结果将是对应的LocalDateTime对象
原文地址: https://www.cveoy.top/t/topic/iQe3 著作权归作者所有。请勿转载和采集!