How to convert ZonedDateTime to Date?

后端 未结 9 691
无人共我
无人共我 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:08

    You can do this using the java.time classes built into Java 8 and later.

    ZonedDateTime temporal = ...
    long epochSecond = temporal.getLong(INSTANT_SECONDS);
    int nanoOfSecond = temporal.get(NANO_OF_SECOND);
    Date date = new Date(epochSecond * 1000 + nanoOfSecond / 1000000);
    

提交回复
热议问题