java.util.Date 包含了日期、时间、毫秒数等
java.time.LocalDate 仅包含日期
Date对象的实例化方式有:
1. new Date()
2. new Date(long date):
通过毫秒数初始化。毫秒数可以通过 java.sql.Timestamp对象的.getTime() 获得
3. Date.from(Instant instant):
通过 Instant 对象初始化。instant为时刻、瞬间的意思
Instant 可以由 ZonedDateTime 或 Timestamp 的实例对象调用 toInstant() 获取
一、Date
转LocalDateTime/LocalDate/LocalTime
Date date = new Date();
// 时区
ZoneId zoneId = ZoneId.systemDefault();
// 方式1
ZonedDateTime zonedDateTime = date.toInstant().atZone(zoneId);
LocalDateTime localDateTime1 = zonedDateTime.toLocalDateTime();
LocalDate localDate1 = zonedDateTime.toLocalDate();
LocalTime localTime1 = zonedDateTime.toLocalTime();
// 方式2
LocalDateTime localDateTime2 = LocalDateTime.ofInstant(date.toInstant(), zoneId);
LocalDate localDate2 = localDateTime2.toLocalDate();
LocalTime localTime2 = localDateTime2.toLocalTime();
二、LocalDateTime/LocalDate/LocalTime
转Date
1. LocalDateTime
转Date
LocalDateTime now = LocalDateTime.now();
// 时区
ZoneId zoneId = ZoneId.systemDefault();
Instant instant = now.atZone(zoneId).toInstant();
Date date = Date.from(instant);
2. LocalDate
转Date
// 由于`LocalDate`不带有时间信息,所以必须设置时间,才能转 Date
LocalDate now = LocalDate.now();
System.out.println(now);
System.out.println("------");
// 时区
ZoneId zoneId = ZoneId.systemDefault();
Instant instant;
Date date;
// 方式1:atStartOfDay,自动赋予午夜时间,返回 LocalDateTime,设置时区返回 ZonedDateTime,进而得到 Instant
instant = now.atStartOfDay().atZone(zoneId).toInstant();
date = Date.from(instant);
System.out.println(date);
// 方式2
instant = now.atStartOfDay(zoneId).toInstant();
date = Date.from(instant);
System.out.println(date);
// 方式3:atStartOfDay 的底层实现
instant = now.atTime(LocalTime.MIDNIGHT).atZone(zoneId).toInstant();
date = Date.from(instant);
System.out.println(date);
// 方式4
instant = LocalDateTime.of(now, LocalTime.MIDNIGHT).atZone(zoneId).toInstant();
date = Date.from(instant);
System.out.println(date);
// 方式5:通过 Timestamp 得到 Instant
instant = Timestamp.valueOf(now.atTime(LocalTime.MIDNIGHT)).toInstant();
date = Date.from(instant);
System.out.println(date);
// 方式6
instant = Timestamp.valueOf(LocalDateTime.of(now, LocalTime.MIDNIGHT)).toInstant();
date = Date.from(instant);
System.out.println(date);
// 方式7:通过毫秒数初始化 Date 对象
Timestamp timestamp = Timestamp.valueOf(now.atTime(LocalTime.MIDNIGHT));
date = new Date(timestamp.getTime());
System.out.println(date);
3. LocalTime
转Date
// 由于 LocalTime 不带有日期信息,所以必须设置日期,才能转 Date
LocalTime nowTime = LocalTime.now();
LocalDate nowDate = LocalDate.now();
System.out.println(nowTime);
System.out.println("------");
// 时区
ZoneId zoneId = ZoneId.systemDefault();
Instant instant;
Date date;
instant = LocalDateTime.of(nowDate, nowTime).atZone(zoneId).toInstant();
date = Date.from(instant);
System.out.println(date);
instant = Timestamp.valueOf(LocalDateTime.of(nowDate, nowTime)).toInstant();
date = Date.from(instant);
System.out.println(date);
Timestamp timestamp = Timestamp.valueOf(LocalDateTime.of(nowDate, nowTime));
date = new Date(timestamp.getTime());
System.out.println(date);