"要将时间戳字符串转换为LocalDateTime对象,可以使用java.time包中的Instant和LocalDateTime类。\n\n下面是一个示例代码:\n\njava\nimport java.time.Instant;\nimport java.time.LocalDateTime;\nimport java.time.ZoneId;\n\npublic class Main {\n public static void main(String[] args) {\n String timestamp = \"1586995200\"; // 时间戳字符串,单位为秒\n\n // 将时间戳转换为Instant对象\n Instant instant = Instant.ofEpochSecond(Long.parseLong(timestamp));\n\n // 将Instant对象转换为LocalDateTime对象\n LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());\n\n System.out.println(localDateTime);\n }\n}\n\n\n在上面的代码中,首先将时间戳字符串转换为秒级别的时间戳,并使用Instant.ofEpochSecond()方法创建一个Instant对象。\n然后,使用LocalDateTime.ofInstant()方法将Instant对象转换为LocalDateTime对象,传入的ZoneId参数使用系统默认时区。\n最后,将转换后的LocalDateTime对象输出。\n\n运行上面的代码,将会输出转换后的LocalDateTime对象。\n"

Java 时间戳字符串转换为LocalDateTime对象 - 代码示例

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

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