1、 Date 之 LocalDate
LocalDate localDate = LocalDate.parse("2020-12-07");
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
System.out.println(LocalDate.parse("2020-10-09").format(pattern));
2、Time之LocalTime
LocalTime localTime = LocalTime.parse("07:43:53");
3、DateTime之 LocalDateTime
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
LocalDate localDate = LocalDate.parse("2019-12-07 07:43:53",formatter);
date time -------- to String
LocalTime localTime = LocalTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm:ss");
String format4 = localTime.format(formatter);
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
String format5 = localDateTime.format(formatter2);
date 《-----------》 LocalDateTime
1、
Date todayDate = new Date();
LocalDateTime ldt = todayDate.toInstant()
.atZone( ZoneId.systemDefault() )
.toLocalDateTime();
2、
LocalDateTime localDateTime = LocalDateTime.now();、
Date date = Date.from( localDateTime.atZone( ZoneId.systemDefault()).toInstant());
- LocalDate 转 Date
ZonedDateTime zdt = localDate.atStartOfDay(ZoneId.systemDefault());
Date date = Date.from(zdt.toInstant());
//LocalDateTime -> Date
public static Date asDate(LocalDateTime localDateTime) {
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
转毫秒
LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
网友评论