LocalDateTime 的长时间戳

2022-01-13 00:00:00 timestamp java-8 java java-time

我有一个长时间戳 1499070300(相当于 2017 年 7 月 3 日星期一 16:25:00 +0800),但是当我将其转换为 LocalDateTime 时,我得到 1970-01-18T16:24:30.300

I have a long timestamp 1499070300 (equivalent to Mon, 03 Jul 2017 16:25:00 +0800) but when I convert it to LocalDateTime I get 1970-01-18T16:24:30.300

这是我的代码

long test_timestamp = 1499070300;

LocalDateTime triggerTime =
                LocalDateTime.ofInstant(Instant.ofEpochMilli(test_timestamp), TimeZone
                        .getDefault().toZoneId());

推荐答案

需要以毫秒为单位传递时间戳:

You need to pass timestamp in milliseconds:

long test_timestamp = 1499070300000L;
LocalDateTime triggerTime =
        LocalDateTime.ofInstant(Instant.ofEpochMilli(test_timestamp), 
                                TimeZone.getDefault().toZoneId());  

System.out.println(triggerTime);

结果:

2017-07-03T10:25

或者使用 ofEpochSecond 代替:

long test_timestamp = 1499070300L;
LocalDateTime triggerTime =
       LocalDateTime.ofInstant(Instant.ofEpochSecond(test_timestamp),
                               TimeZone.getDefault().toZoneId());   

System.out.println(triggerTime);

结果:

2017-07-03T10:25

相关文章