import java.time.*; import java.util.Date; /** * @Auther kejiefu * @Date 2018/5/17 0017 */ public class DateUtils { /** * 将LocalDateTime转换成Date * * @param localDateTime * @return date */ public static Date convertLocalDateTimeToDate(LocalDateTime localDateTime) { ZoneId zoneId = ZoneId.systemDefault(); ZonedDateTime zdt = localDateTime.atZone(zoneId); Date date = Date.from(zdt.toInstant()); return date; } /** * 将Date转换成LocalDateTime * * @param date * @return LocalDateTime */ public static LocalDateTime convertDateToLocalDateTime(Date date) { Instant instant = date.toInstant(); ZoneId zoneId = ZoneId.systemDefault(); LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime(); return localDateTime; } }
LocalDateTime计算时间差
java.time.Duration duration = java.time.Duration.between(LocalDateTime startTime, LocalDateTime endTime ); 例如: duration.toMinutes() //两个时间差的分钟数 toNanos()//纳秒 toMillis()//毫秒 toMinutes()//分钟 toHours()//小时 toDays()//天数