1.8
的LocalDate
和Date
之间的相互转换需要借助Date
中新增的from()
和toInstant()
LocalDate 转Date
1.需要先将LocalDate
转为LocalDateTime
LocalDate.atStartOfDay()
2. 获取 Instance
,需要指定时区,这里使用系统默认
LocalDateTime.atZone(ZoneId.systemDefault()).toInstant()
3.转化
Date.from()
连起来就是
Date.from(LocalDate.now().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant())
Date转LocalDate
new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()