package com.fourdage.dingding.util;

import cn.hutool.core.date.DateUtil;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * Created by owen on 2022/4/8 0008 14:32
 */

public class DateUtils extends DateUtil {
    public Logger log = LoggerFactory.getLogger(getClass());

    private static String YYYY_MM = "yyyy-MM";

    private static String YYYYMM = "yyyyMM";

    public static String YYYYMMDD = "yyyyMMdd";

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

    private static String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";


    private static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    private static String YYYYMMDD_HHMMSSSSS = "yyyyMMdd_HHmmssSSS";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static String YYYY_MM_DD_HH_MM_SS_2 = "yyyy/MM/dd HH:mm:ss";


    /**
     *
     * @param time 字符串日期,必须为2022-02-16 12:26:00
     * @param format 格式
     * @return
     */
    public static String formatStr(String time, String format){
        // 将原始日期字符串解析为Date对象
        Date date = DateUtil.parseDateTime(time);
        String s = format(date, format);

        return s;
    }


    /**
     * 获取当前月
     * @return
     */
    public static String getMonth(){
        return format(LocalDateTime.now(), YYYYMM);
    }

    /**
     * 获取当前月份的前几个月
     * @param month
     * @return
     */
    public static String minusMonths(int month){
        return format(LocalDateTime.now().minusMonths(month), YYYYMM);
    }

    /**
     * 获取当前时间戳
     * @return
     */
    public static String getDateTime(){
        return format(LocalDateTime.now(), YYYYMMDD_HHMMSSSSS);
    }


    /**
     * 字符串转LocalDateTime
     * @param time 字符串
     * @return LocalDateTime
     */
    public static LocalDateTime srtToLocalDateTime(String time){
        return LocalDateTime.parse(time, DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM));
    }

    public static LocalDate srtToLocalDate(String time){
        return LocalDate.parse(time, DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS));
    }

    @Test
    public void test(){
        System.out.println(formatStr("2022-02-16 12:26:00", YYYYMMDD));
    }


    public static String getStrTime(long timestamp){
        // 将时间戳转换为Instant对象
        Instant instant = Instant.ofEpochMilli(timestamp);

        // 可选：将Instant转换到特定时区的ZonedDateTime，这里是使用系统默认时区
        // ZoneId zoneId = ZoneId.systemDefault();
        // ZonedDateTime zonedDateTime = instant.atZone(zoneId);

        // 将Instant转换为LocalDateTime，如果你不需要时区信息
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());

        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 使用定义的格式器来格式化日期时间
        String formattedDateTime = localDateTime.format(formatter);
        return formattedDateTime;
    }

}
