很长的时间戳记LocalDateTime


问题内容

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

这是我的代码

long test_timestamp = 1499070300;

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

问题答案:

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

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