Java 8: How to create a ZonedDateTime from an Epoc

2019-03-11 14:22发布

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条回答
兄弟一词,经得起流年.
2楼-- · 2019-03-11 15:16

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);
查看更多
登录 后发表回答