Java 程序可将 java.util.Date 转换为 LocalDate
首先用 java.util.Date 设置日期 -
java.util.Date date = new Date();
现在,将日期转换为 LocalDate -
Instant instant = Instant.ofEpochMilli(date.getTime()); System.out.println("LocalDate = "+LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate());
示例
import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Date; public class Demo { public static void main(String[] args) { java.util.Date date = new Date(); System.out.println("Date = "+date); Instant instant = Instant.ofEpochMilli(date.getTime()); System.out.println("LocalDate = "+LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate()); } }
输出
Date = Thu Apr 18 23:51:06 IST 2019 LocalDate = 2019-04-18
广告