LocalDate LocalDateTime Date 转换

1、LocalDateTime --> Date

   eg:  Instant ins = LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant();

         Date date = Date.from(ins);

2、LocalDate -->Date

     首先将LocalDate 转 LocalDateTime  ,  eg: LocalDateTime = LocalDate.now().atStartOfDay(); 然后同上;

3、Date  --> LocalDate

   eg: LocalDate localDate = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault().toLocalDate());

4、Date  --> LocalDateTime

   eg:LocalDateTime localDateTime = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault());

5、字符串日期转 LocalDate 和 LocalDateTime

     eg:  String dateStr = "2019-10-01 12:00:00";

LocalDate localDate = LocalDate.parse("2019-10-01 12:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
LocalDate localDate1 = LocalDate.parse("2019-10-01");
LocalDate localDate2 = LocalDate.parse("2019/10/01", DateTimeFormatter.ofPattern("yyyy/MM/dd"));

LocalDateTime localDateTime = LocalDateTime.parse("2019-10-01 12:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

注意:DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")  的格式要与日期字符串格式相互匹配

 

你可能感兴趣的:(java)