java.time.LocalDate到java.util.Date [英] java.time.LocalDate to java.util.Date
问题描述
从java.time.LocalDate
转换为java.util.Date
的最佳方法是什么?
Date.from(dateToReturn.atStartOfDay(ZoneId.systemDefault()).toInstant()
我一直在尝试这种方法,但是尽管可以正确地转换日期和年份,但似乎不能正确显示时间.
更新:java.time.LocalDate不保存时间信息只是使用了java.time.LocalDateTime,一切正常.
LocalDate ld = ...;
Instant instant = ld.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
Date res = Date.from(instant);
查看此博客文章,在Date和java8 java.time.LocalDateTime之间进行转换,LocalDate和LocalTime .
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.
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.
这篇关于java.time.LocalDate到java.util.Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!