Java把LocalDate转换为Date

时间:2022-06-01 20:50:24

有时需要在LocalDate和Date之间互转。

LocalDate转换为Date

LocalDate localDate = LocalDate.now();
System.out.println("LocalDate = " + localDate);
Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
System.out.println("Date      = " + date);

Date转换为LocalDate

localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
System.out.println("LocalDate = " + localDate);