How to convert ZonedDateTime to Date?

后端 未结 9 683
无人共我
无人共我 2020-12-04 09:19

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

9条回答
  •  粉色の甜心
    2020-12-04 10:10

    If you are using the ThreeTen backport for Android and can't use the newer Date.from(Instant instant) (which requires minimum of API 26) you can use:

    ZonedDateTime zdt = ZonedDateTime.now();
    Date date = new Date(zdt.toInstant().toEpochMilli());
    

    or:

    Date date = DateTimeUtils.toDate(zdt.toInstant());
    

    Please also read the advice in Basil Bourque's answer

提交回复
热议问题