Java 到 LocalDateTime 的长时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44883432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
long timestamp to LocalDateTime
提问by rhandom
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
我有一个很长的时间戳 1499070300(相当于 2017 年 7 月 3 日星期一 16:25:00 +0800)但是当我将其转换为 LocalDateTime 时,我得到 1970-01-18T16:24:30.300
Here's my code
这是我的代码
long test_timestamp = 1499070300;
LocalDateTime triggerTime =
LocalDateTime.ofInstant(Instant.ofEpochMilli(test_timestamp), TimeZone
.getDefault().toZoneId());
采纳答案by Juan
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);
Result:
结果:
2017-07-03T10:25
Or use ofEpochSecond
instead:
或者ofEpochSecond
改用:
long test_timestamp = 1499070300L;
LocalDateTime triggerTime =
LocalDateTime.ofInstant(Instant.ofEpochSecond(test_timestamp),
TimeZone.getDefault().toZoneId());
System.out.println(triggerTime);
Result:
结果:
2017-07-03T10:25
回答by Razib
Try with Instant.ofEpochMilli()
or Instant.ofEpochSecond()
method with it-
尝试Instant.ofEpochMilli()
或Instant.ofEpochSecond()
使用它的方法-
long test_timestamp = 1499070300L;
LocalDateTime date =
LocalDateTime.ofInstant(Instant.ofEpochMilli(test_timestamp ), TimeZone
.getDefault().toZoneId());
回答by Akshay
Try with the following..
尝试以下..
long test_timestamp = 1499070300000L;
LocalDateTime triggerTime =
LocalDateTime.ofInstant(Instant.ofEpochMilli(test_timestamp), TimeZone
.getDefault().toZoneId());
By default 1499070300000
is int if it dosen't contain l in end.Also pass time in milliseconds.
1499070300000
如果最后不包含 l,则默认为 int。也以毫秒为单位传递时间。
回答by Alex Roig
Your issue is that the timestamp is not in milliseconds but expressed in seconds from the Epoch date. Either multiply by 1000 your timestamp or use the Instant.ofEpochSecond()
.
您的问题是时间戳不是以毫秒为单位,而是以从纪元日期开始的秒数表示。将时间戳乘以 1000 或使用Instant.ofEpochSecond()
.
回答by JamieH
If you are using the Android threeten back port then the line you want is this
如果您使用的是 Android Threeten back port 那么您想要的线路是这样的
LocalDateTime.ofInstant(Instant.ofEpochMilli(startTime), ZoneId.systemDefault())