如何将ZonedDateTime转换为日期? [英] How to convert ZonedDateTime to Date?

查看:1676
本文介绍了如何将ZonedDateTime转换为日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的数据库中设置服务器不可知日期时间,我相信这样做的最佳做法是设置UTC日期时间。我的数据库服务器是Cassandra,Java的db驱动程序只能理解Date类型。

I am trying to set a server agnostic date time in my database and I believe the best practice to do so is to set a UTC DateTime. My db server is Cassandra and the db driver for Java understands only the Date type.

所以假设在我的代码中我使用新的Java 8 ZonedDateTime来获取UTC now( ZonedDateTime.now(ZoneOffset.UTC)),如何将此ZonedDateTime实例转换为遗留Date类?

So assuming that in my code I am using the new Java 8 ZonedDateTime to get the UTC now (ZonedDateTime.now(ZoneOffset.UTC)), how can I convert this ZonedDateTime instance to the "legacy" Date class?

推荐答案

您可以将ZonedDateTime转换为瞬间,您可以直接使用日期。

You can convert ZonedDateTime to an instant, which you can use directly with Date.

Date.from(java.time.ZonedDateTime.now().toInstant());

这篇关于如何将ZonedDateTime转换为日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆