java8的LocalDateTime与Date互相转换

LocalDateTime转Date

Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
Date resultDate = Date.from(instant);

Date转LocalDateTime

LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();

LocalDateTime常用操作

LocalDateTime localDateTime = dateTime.plusMonths(n).withHour(23).withMinute(59).withSecond(59).withNano(0);// 加n个月,设置23点59分59秒
LocalDateTime localDateTime = dateTime.plusMonths(n).with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59).withNano(0);// 加n个月,月底那天

猜你喜欢

转载自www.cnblogs.com/ouyida3/p/9032585.html