Date和LocalDateTime互转

127 阅读1分钟
public class TimeUtils {

    /**
     * Date转换成LocalDateTime
     *
     * @param date
     * @return
     */
    public static LocalDateTime transferToLocalDateTime(Date date) {
        Instant instant = date.toInstant();

        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = instant.atZone(zoneId);
        LocalDateTime localDateTime = zdt.toLocalDateTime();

        return localDateTime;
    }

    /**
     * LocalDateTime转换成Date
     *
     * @param localDateTime
     * @return
     */
    public static Date transferToDate(LocalDateTime localDateTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);

        Instant instant = zdt.toInstant();
        Date date = Date.from(instant);
        return date;
    }
}