一、Date与LocalDateTime、LocalDate、LocalTime互转
1、Date转LocalDateTime、LocalDate、LocalTime
LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); LocalTime localTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalTime(); LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
上面很容易看出,无论是date转哪个,都是首先date.toInstant().atZone(ZoneId.systemDefault()),然后是相应的toLocalDate()、toLocalTime()、toLocalDateTime()
2、LocalDateTime、LocalDate、LocalTime转Date
Date date1 = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); Date date2 = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); Date date3 = Date.from(localTime.atDate(LocalDate.now()).atZone(ZoneId.systemDefault()).toInstant());
上面可以看出,
- LocalDateTime可以直接转:Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
- LocalDate需要先调用atStartOfDay()设置默认时间,然后再转:Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
- LocalTime需要先设置日期转成LocalDateTime再转:Date.from(localTime.atDate(LocalDate.now()).atZone(ZoneId.systemDefault()).toInstant());
二、LocalDateTime的常规操作
1、LocalDateTime 初始化定义
LocalDateTime now = LocalDateTime.now(); LocalDateTime localDateTime=LocalDateTime.of(LocalDate.now(),LocalTime.now());//LocalDate和LocalTime合成LocalDateTime LocalDateTime of = LocalDateTime.of(2020, 11, 11, 20, 30, 40);
2、LocalDateTime 日期加减
LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 11, 11, 59);//2020-11-11 11:11:59 LocalDateTime localDateTime1 = localDateTime.plusSeconds(1);//2020-11-11 11:11:59 +1秒,localDateTime1=2020-11-11 11:12:00 LocalDateTime localDateTime2 = localDateTime.plusSeconds(-1);//2020-11-11 11:11:59 -1秒,localDateTime2=2020-11-11 11:11:58 LocalDateTime localDateTime3 = localDateTime.plusMinutes(1);//2020-11-11 11:11:59 +1分钟,localDateTime3=2020-11-11 11:12:59 LocalDateTime localDateTime4 = localDateTime.plusMinutes(-1);//2020-11-11 11:11:59 -1分钟,localDateTime4=2020-11-11 11:10:59 LocalDateTime localDateTime5 = localDateTime.plusHours(1);//2020-11-11 11:11:59 +1小时,localDateTime5=2020-11-11 12:11:59 LocalDateTime localDateTime6 = localDateTime.plusHours(-1);//2020-11-11 11:11:59 -1小时,localDateTime6=2020-11-11 10:11:59 LocalDateTime localDateTime7 = localDateTime.plusDays(1);//2020-11-11 11:11:59 +1天,localDateTime7=2020-11-12 11:11:59 LocalDateTime localDateTime8 = localDateTime.plusDays(-1);//2020-11-11 11:11:59 -1天,localDateTime8=2020-11-10 11:11:59 LocalDateTime localDateTime9 = localDateTime.plusMonths(1);//2020-11-11 11:11:59 +1月,localDateTime9=2020-12-11 11:11:59 LocalDateTime localDateTime10 = localDateTime.plusMonths(-1);//2020-11-11 11:11:59 -1月,localDateTime10=2020-10-11 11:11:59 LocalDateTime localDateTime11 = localDateTime.plusYears(1);//2020-11-11 11:11:59 +1年,localDateTime11=2021-11-11 11:11:59 LocalDateTime localDateTime12 = localDateTime.plusYears(-1);//2020-11-11 11:11:59 -1年,localDateTime12=2019-11-11 11:11:59
除了上面年月日时分秒的加减,还有周的加减plusWeeks(),纳秒的加减plusNanos()
plusNanos()的使用可参考链接https://www.yiibai.com/javatime/javatime_localtime_plusnanos.html
3、LocalDateTime 格式化
LocalDateTime localDateTime=LocalDateTime.now();
localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
格式化 HH是24小时制,hh是12小时制
LocalDateTime还有很多方法可以使用,比如获取月份等,可以看源码。