Java中String、LocalDateTime、LocalDate、Date互转

String 转LocalDate和LocalDateTime

LocalDate startDate =
                    LocalDate.parse("2019-12-05", DateTimeFormatter.ofPattern("yyyy-MM-dd"));

LocalDateTime startDateTime =
        LocalDateTime.parse("2019-12-05 15:30:11", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

LocalDate转Date

LocalDate localDate = LocalDate.now();
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
java.util.Date date = Date.from(instant);

LocalDateTime转Date

Date LocalDateTimeToDate = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());

LocalDateTime转String 

String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

 

更多转换格式请参考:

https://www.cnblogs.com/bestzrz/p/10425870.html

https://www.cnblogs.com/exmyth/p/6425878.html

posted @ 2019-12-05 16:23  代码猫  阅读(25869)  评论(0编辑  收藏  举报