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

ぐ巨炮叔叔 提交于 2019-12-03 06:28:16

问题


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?


回答1:


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 i = Instant.ofEpochSecond(t);
ZonedDateTime z = ZonedDateTime.ofInstant(i, zoneId);


来源:https://stackoverflow.com/questions/28808305/java-8-how-to-create-a-zoneddatetime-from-an-epoch-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!