java.time.LocalDate to java.util.Date

陌路散爱 提交于 2019-12-13 04:08:39

问题


What is the best way to convert from java.time.LocalDate to java.util.Date ?

Date.from(dateToReturn.atStartOfDay(ZoneId.systemDefault()).toInstant()

I have been trying this one but does not seem to work correct with the time although converts the date month and year correctly.

Update : java.time.LocalDate does not save time informations Just used java.time.LocalDateTime instead and everything works fine.


回答1:


LocalDate ld = ...;
Instant instant = ld.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
Date res = Date.from(instant);

Check out this blog post, Converting between Date and java8 java.time.LocalDateTime, LocalDate and LocalTime by joachim.



来源:https://stackoverflow.com/questions/27250666/java-time-localdate-to-java-util-date

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