package com.jwipc.util;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.lang.Pair;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

/**
 * <p> 时间工具类</p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
public class DateUtils {
    public final static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd[ HH:mm:ss]");
    public final static String yyyyMMdd = "yyyyMMdd";
    public final static String yyMMdd = "yyMMdd";
    public final static String yyyyMMddDash = "yyyy-MM-dd";
    public final static String yyyyMMddSlash = "yyyy/MM/dd";
    public final static String yyyyMMDash = "yyyy-MM";
    public final static String YYYY_MMDD_HMS = "yyyy-MM-dd HH:mm:ss";

    public static String formatYYYYMMDD(Date date) {
        return cn.hutool.core.date.DateUtil.format(date, yyyyMMdd);
    }

    public static String formatYYYYMMDD(LocalDate date) {
        return LocalDateTimeUtil.format(date, yyyyMMdd);
    }

    public static String formatYYYYMMDDSlash(LocalDateTime date) {
        return LocalDateTimeUtil.format(date, yyyyMMddSlash);
    }

    public static String formatYYYYMMDD(LocalDateTime date) {
        return LocalDateTimeUtil.format(date, yyyyMMdd);
    }

    public static String formatYYYYMMDDDash(Date date) {
        return cn.hutool.core.date.DateUtil.format(date, yyyyMMddDash);
    }

    public static String formatYYYYMMDDDash(LocalDate date) {
        return LocalDateTimeUtil.format(date, yyyyMMddDash);
    }
    public static String formatYYYYMMDDDash(LocalDateTime date) {
        return LocalDateTimeUtil.format(date, yyyyMMddDash);
    }

    public static String formatYYYYMMDDHms(Date date) {
        return cn.hutool.core.date.DateUtil.format(date, YYYY_MMDD_HMS);
    }

    public static String formatYYYYMMDDHms(LocalDateTime date) {
        return LocalDateTimeUtil.format(date, YYYY_MMDD_HMS);
    }

    public static String formatYYMMDD(LocalDate date) {
        return LocalDateTimeUtil.format(date, yyMMdd);
    }

    public static Pair<LocalDateTime, LocalDateTime> getStartAndEndDate(Integer year, Integer month) {
        final LocalDateTime localDateTime = LocalDateTime.of(year, month, 1, 0, 0);
        // 获取该月的第一天（最小时间）
        LocalDateTime firstDayOfMonth = localDateTime.withDayOfMonth(1);
        // 获取该月的最后一天（最大时间）
        LocalDateTime lastDayOfMonth = localDateTime.with(TemporalAdjusters.lastDayOfMonth());
        return Pair.of(LocalDateTimeUtil.beginOfDay(firstDayOfMonth), LocalDateTimeUtil.endOfDay(lastDayOfMonth));
    }

    public static LocalDateTime formatYYYYMMDDDash(String dateTimeString) {
        dateTimeString = dateTimeString + "T00:00:00";
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);
        return dateTime;
    }

    public static LocalDate formatYYYYMMDD(String dateTimeString) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(yyyyMMdd);
        LocalDate date = LocalDate.parse(dateTimeString,formatter);
        return date;
    }

    public static void main(String[] args) {
        String ss = "20250329";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(yyyyMMdd);
        LocalDate da = LocalDate.parse(ss,formatter);
        System.out.println(DateUtils.formatYYYYMMDDDash(ss));
    }

}
