package com.dxhy.core.util;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 时间转换Utils
 */
public class DateUtils {
    private static ThreadLocal<SimpleDateFormat> simpleDateFormat_yyyymm = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMM"));
    private static ThreadLocal<SimpleDateFormat> simpleDateFormat = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMdd"));

    /**
     * 获取当月的第一天
     */
    public static String getMonth() {
        Calendar cal = Calendar.getInstance();//获取当前日期
        cal.add(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);//设置为1号
        String format = simpleDateFormat.get().format(cal.getTime());
        return format;
    }

    /**
     * 获取当前月上个月第一天
     */
    public static String getLastMonth() {
        Calendar cal = Calendar.getInstance();//获取当前日期
        cal.add(Calendar.MONTH, -1);
        cal.set(Calendar.DAY_OF_MONTH, 1);//设置为1号
        String format = simpleDateFormat.get().format(cal.getTime());
        return format;
    }


    /**
     * 获取当前月上个月最后一天
     */
    public static String getLastMonthEnd() {
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        String format = simpleDateFormat.get().format(calendar.getTime());
        return format;
    }

    /**
     *  获取某一个日期该月第一天
     * @param StartTime 格式为yyyyMMdd
     * @return
     * @throws ParseException
     */
    public static String getMonthStart(String StartTime) throws ParseException {
        Date date = simpleDateFormat.get().parse(StartTime);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, 1);//设置为1号
        String format = simpleDateFormat.get().format(cal.getTime());
        return format;
    }

    /**
     * 获取某一个日期该月最后一天
     * @param endTime 格式为yyyyMM
     * @return
     * @throws ParseException
     */
    public static String getMonthEnd(String endTime) throws ParseException {
        Date date = simpleDateFormat_yyyymm.get().parse(endTime);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        String format = simpleDateFormat.get().format(cal.getTime());
        return format;
    }

    /**
     * 获取某一个日期该月最后一天
     * @param endTime 格式为yyyyMMdd
     * @return
     * @throws ParseException
     */
    public static String getMonthEndyyyyMMdd(String endTime) throws ParseException {
        Date date = simpleDateFormat.get().parse(endTime);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        String format = simpleDateFormat.get().format(cal.getTime());
        return format;
    }

    /**
     * 判断当前日期是否为当前月yyyyMMdd
     */
    public static boolean isThisTime(String endTime) throws ParseException {
        Date date = simpleDateFormat.get().parse(endTime);
        String param = simpleDateFormat_yyyymm.get().format(date);//参数时间
        String now = simpleDateFormat_yyyymm.get().format(new Date());//当前时间
        if (param.equals(now)) {
            return true;
        }
        return false;
    }


    /**
     * 判断当前日期是否为当前月
     */
    public static boolean isTime(Date date) throws ParseException {
        String param = simpleDateFormat_yyyymm.get().format(date);//参数时间
        String now = simpleDateFormat_yyyymm.get().format(new Date());//当前时间
        if (param.equals(now)) {
            return true;
        }
        return false;
    }
    /**
     * 获取当前日期的前n天
     */
    public static String getDate(int n) throws ParseException {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) - n);
        String format = simpleDateFormat.get().format(cal.getTime());
        return format;
    }

    /**
     * 当前日期月份加 1
     * @param month 格式为yyyyMMdd
     * @return
     * @throws ParseException
     */
    public static String addMonth(String month) throws ParseException {
        Calendar cld = Calendar.getInstance();
        cld.setTime(simpleDateFormat.get().parse(month));
        cld.add(Calendar.MONTH, +1);
        return simpleDateFormat.get().format(cld.getTime());
    }

    /**
    * @Description 格式化日期为字符串
    * @Return java.lang.String
    * @Author wangruwei
    * @Date 2022/11/15 11:40
    **/
    public static String dateToStr(Date dateDate, String format) {
        if (dateDate == null) {
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(dateDate);
    }
    /**
     * @Description 字符串类型的日期转为date,字符串和格式长度需一样
     * @Return java.lang.String
     * @Author wangruwei
     * @Date 2022/11/15 11:40
     **/
    public static Date strToDate(String strDate,String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        ParsePosition pos = new ParsePosition(0);
        return formatter.parse(strDate, pos);
    }

    /**
    * @Description 获取所传日期到当前日期的所有月的开始日期结束日期集合
     * 今日20221115，传入20221003，返回
     * [20221001@20221031, 20221101@20221130]
    * @Return java.util.List<java.lang.String>
    * @Author wangruwei
    * @Date 2022/11/23 10:08
    **/
    public static List<String> getStarToNowMonth(Date startDate) throws ParseException {
        List<String> list = new LinkedList<String>();
        String starStr = dateToStr(startDate, "yyyyMM");
        String nowStr = dateToStr(new Date(), "yyyyMM");
        //如果当前月大于等于开始月
        String startrq = starStr+"01";
        if(nowStr.compareTo(starStr)>=0){
            list.add(getMonthStart(startrq)+"@"+getMonthEndyyyyMMdd(startrq));

            while (true){
                //如果这个月是当前月，在上述已经入list，跳出
                boolean isThisMonth = isThisTime(startrq);
                if(isThisMonth){
                    break;
                }
                String nextMonth = addMonth(startrq);
                startrq = nextMonth;

                list.add(getMonthStart(startrq)+"@"+getMonthEndyyyyMMdd(startrq));
                //如果下个月大于这个月，跳出
                if(nextMonth.compareTo(nowStr)>0){
                    break;
                }
            }
        }
        return list;
    }



    public static void main(String[] args) throws ParseException {
        String monthEnd = addMonth("20231201");
        List<String> starToNowMonth = getStarToNowMonth(strToDate("20230401", "yyyyMMdd"));
        //List<String> starToNowMonth = getStarToNowMonth(new Date());
        System.out.println(starToNowMonth);
        if(true){
            return;
        }


        String startrq = "20170101";
        //判断开始日期是否高于现在日期
        //if(strToDate("20170101", "yyyyMM")){
        //
        //};
        //System.out.println(addMonth(startrq));
        //是否为现在当月
        boolean thisTime = isThisTime("20221115");
        while (true){
            String nextMonth = addMonth(startrq);
            startrq = nextMonth;
            //System.out.println(startrq);
            System.out.print(getMonthStart(startrq)+"   ");
            System.out.println(getMonthEnd(startrq));
            boolean isThisMonth = isThisTime(nextMonth);
            if(isThisMonth){
                break;
            }
        }
        

    }
}
