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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:51:24  来源:igfitidea点击:

Java 8: How to create a ZonedDateTime from an Epoch value?

javajava-8epochzoneddatetime

提问by rabejens

Java 8's LocalDateTimehas an ofEpochSecondmethod. Unfortunately, there is no such method in the ZonedDateTime. Now, I have an Epoch value and an explicit ZoneIdgiven. How do I get a ZonedDateTimeout of these?

Java 8LocalDateTime有一个ofEpochSecond方法。不幸的是,在ZonedDateTime. 现在,我有一个 Epoch 值和一个明确的ZoneId给定值。我如何ZonedDateTime摆脱这些?

采纳答案by Prisoner

You should be able to do this via the Instantclass, 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);