Java 8:如何从 Epoch 值创建 ZonedDateTime?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28808305/
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
Java 8: How to create a ZonedDateTime from an Epoch value?
提问by rabejens
Java 8's LocalDateTime
has an ofEpochSecond
method. Unfortunately, there is no such method in the ZonedDateTime
. Now, I have an Epoch value and an explicit ZoneId
given. How do I get a ZonedDateTime
out of these?
Java 8LocalDateTime
有一个ofEpochSecond
方法。不幸的是,在ZonedDateTime
. 现在,我有一个 Epoch 值和一个明确的ZoneId
给定值。我如何ZonedDateTime
摆脱这些?
采纳答案by Prisoner
You should be able to do this via the Instant
class, which can represent a moment given the epoch time. If you have epoch seconds, you might create something via something like
您应该能够通过Instant
类来做到这一点,它可以代表给定纪元时间的一个时刻。如果你有纪元秒,你可能会通过类似的东西创建一些东西
Instant i = Instant.ofEpochSecond(t);
ZonedDateTime z = ZonedDateTime.ofInstant(i, zoneId);