package com.ruoyi.common.core.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class LocalDateUtils {
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    /**
     * String转LocalDate
     * @param dateStr        日期的字符串
     * @param pattern        格式示例： yyyy-MM-dd
     * @return
     */
    public static LocalDate toLocalDate(String dateStr, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return LocalDate.parse(dateStr, formatter);
    }

    /**
     * LocalDate转String
     * @param localDate   localDate对象
     * @param pattern        格式示例： yyyy-MM-dd
     * @return
     */
    public static String LocalDatetoStr(LocalDate localDate, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return localDate.format(formatter);
    }
    /**
     * LocalDate转Date
     * @param localDate
     * @return
     */
    public static Date toDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * Date转LocalDate
     * @param date
     * @return
     */
    public static LocalDate toLocalDate(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }

    /**
     * String转LocalDateTime
     * @param dateTimeStr 日期的字符串
     * @param pattern        格式示例： yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static LocalDateTime toLocalDateTime(String dateTimeStr, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return LocalDateTime.parse(dateTimeStr, formatter);
    }

    /**
     * LocalDateTime转String
     * @param localDateTime 日期时间对象
     * @param pattern             格式示例： yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String LocalDateTimetoStr(LocalDateTime localDateTime, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return localDateTime.format(formatter);
    }
    /**
     * Date转LocalDateTime
     * @param date          jdk6 日期对象
     * @param zoneId        时区id
     * @return
     */
    public static LocalDateTime toLocalDateTime(Date date, ZoneId zoneId) {
        // toInstant()：将Date对象转换成为Instant(瞬时)对象
        // ofInstant(): 将瞬时对象转换成为LocalDateTime对象
        Instant instant = date.toInstant();
        if (zoneId == null) {
            zoneId = ZoneId.systemDefault();
        }
        return LocalDateTime.ofInstant(instant, zoneId);
    }

    /**
     * LocalDateTime转Date
     * @param localDateTime  日期时间对象
     * @return
     */
    public static Date LocalDateTimetoDate(LocalDateTime localDateTime, ZoneId zoneId) {
        if (zoneId == null) {
            zoneId = ZoneId.systemDefault();
        }
        ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
        Instant instant = zonedDateTime.toInstant();
        return Date.from(instant);
    }
}
