LocalDateTime的长时间戳 [英] long timestamp to LocalDateTime

查看:126
本文介绍了LocalDateTime的长时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很长的时间戳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

这篇关于LocalDateTime的长时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆