JAVA8 LocalDateTime 与 Date 互相转换

public static Date DateAddYearsByLocalDateTime(Date date,Long years){
   //将Date 转换LocalDateTime
   Instant instant = date.toInstant();
   ZoneId zoneId = ZoneId.systemDefault();
   LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();

   //localDateTime + N年
   LocalDateTime plusYearsResult = localDateTime.plusYears(years);

   //LocalDateTime转Date
   ZoneId zoneId2 = ZoneId.systemDefault();
   ZonedDateTime zdt = plusYearsResult.atZone(zoneId2);
   return  Date.from(zdt.toInstant());
}

你可能感兴趣的:(JAVA)