package com.zcxk.util;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * jdk1.8 LocalDateTime 时间操作工具类
 */
public class Jdk8DateUtils {

    /**
     * LocalDateTime 转 Date
     *
     * @param localDateTime
     * @return
     */
    public static Date getLocalDateTimeToDate(LocalDateTime localDateTime) {
        if (localDateTime != null) {
            ZoneId zoneId = ZoneId.systemDefault();
            return Date.from(localDateTime.atZone(zoneId).toInstant());
        }else {
            return null;
        }
    }

    /**
     * LocalDate 转 Date
     *
     * @param localDate
     * @return
     */
    public static Date getLocalDateToDate(LocalDate localDate) {
        if (localDate != null) {
            ZoneId zoneId = ZoneId.systemDefault();
            return Date.from(localDate.atStartOfDay(zoneId).toInstant());
        }else {
            return null;
        }
    }


    /**
     * Date 转 LocalDateTime
     *
     * @param date
     * @return
     */
    public static LocalDateTime getDateToLocalDateTime(Date date) {
        if (date != null) {
            ZoneId zoneId = ZoneId.systemDefault();
            Instant instant = date.toInstant();
            return LocalDateTime.ofInstant(instant, zoneId);
        }else {
            return null;
        }
    }

}
