LocalDate和Date类型相互转换

1,611 阅读1分钟

1.8LocalDateDate之间的相互转换需要借助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()