How can we convert com.datastax.driver.core.LocalDate to java.util.Date?

China☆狼群 提交于 2019-12-13 14:18:03

问题


I am working with dates.

Datastax's CQL cassandra API Row.getDate() returns a com.datastax.driver.core.LocalDate.

I want to convert the com.datastax.driver.core.LocalDate object returned by the API to java.util.Date. How can I do that?


回答1:


The LocalDate.getMillisSinceEpoch() Javadoc says Returns the number of milliseconds since January 1st, 1970 GMT. And, the Date(long) constructor Javadoc says Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT. So, given a LocalDate ld you should be able to do something like

Date d = new Date(ld.getMillisSinceEpoch());



回答2:


As the other answer mentions either you can use

Date d = new Date(row.getDate().getMillisSinceEpoch());

or you can use java.sql.Date.valueOf

Date d = java.sql.Date.valueOf(row.getDate().getString());

Instead of using row.getDate I personally use row.getTimestamp which returns Date object itself.

Date d = row.getTimestamp();



来源:https://stackoverflow.com/questions/33295137/how-can-we-convert-com-datastax-driver-core-localdate-to-java-util-date

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