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"));
更多转换格式请参考: