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());

原文由 rhandom 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 792
2 个回答

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

 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

原文由 Juan 发布,翻译遵循 CC BY-SA 3.0 许可协议

如果您使用的是 Android threeten back 端口,那么您想要的行是

LocalDateTime.ofInstant(Instant.ofEpochMilli(startTime), ZoneId.systemDefault())

原文由 JamieH 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题