java.time.LocalDate到java.util.Date [英] java.time.LocalDate to java.util.Date

查看:122
本文介绍了java.time.LocalDate到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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆