package com.sonic.frog.utils;


import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.joda.time.DateTime;
import org.joda.time.Days;

import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.time.chrono.IsoChronology;
import java.util.*;

import static org.reflections.Reflections.log;

/**
 * 严格的日期转换setLenient(false); setLenient public void setLenient(boolean
 * lenient)指定日期/时间解析是否不严格。进行不严格解析时，解析程序可以使用启发式的方法来解释与此对象的格式不精确匹配的输入。进行严格解析时， 输入必须匹配此对象的格式。 参数：
 * lenient - 为 true 时，解析过程是不严格的 不会自动将错误日期转换为正确的日期 例如:19450000,使用原DateUtil会转换为19441130
 */
public class DateUtil {
    public static final String COMPACT_DATE_FORMAT = "yyyyMMdd";
    public static final String YM = "yyyyMM";
    public static final String YMD = "yyyyMMdd";
    public static final String YMDHMS = "yyyyMMddHHmmss";
    public static final String NORMAL_DATE_FORMAT = "yyyy-MM-dd";
    public static final String NORMAL_DATE_FORMAT_NEW = "yyyy-mm-dd hh24:mi:ss";
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_ALL = "yyyyMMddHHmmssS";
    public static final String DETE_ALL_SIX = "yyMMddHHmmssSSSSSS";
    public static final String DATE_FORMAT_SLASH = "yyyy/MM/dd";
    public static final String DATETIME_FORMAT_SLASH = "yyyy/MM/dd HH:mm:ss";
    public static final String DATETIME_FORMAT_T = "yyyy-MM-ddTHHmmss";
    public static final String DATETIME_FORMAT_START = "yyyy-MM-dd 00:00:00";
    public static final String DATETIME_FORMAT_END = "yyyy-MM-dd 23:59:59";
    public static final String DATETIME_FORMAT_ZH = "yyyy年MM月dd日";

    public static final ZoneId ZONE_ID = ZoneId.systemDefault();

    private static final SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    public static Long strDateToNum(String paramString) throws Exception {
        if (paramString == null) {
            return null;
        }
        String[] arrayOfString = null;
        String str = "";
        if (paramString.indexOf("-") >= 0) {
            arrayOfString = paramString.split("-");
            for (int i = 0; i < arrayOfString.length; ++i) {
                str = str + arrayOfString[i];
            }
            return Long.valueOf(Long.parseLong(str));
        }
        return Long.valueOf(Long.parseLong(paramString));
    }

    public static Long strDateToNum1(String paramString) throws Exception {
        if (paramString == null) {
            return null;
        }
        String[] arrayOfString = null;
        String str = "";
        if (paramString.contains("-")) {
            arrayOfString = paramString.split("-");
            for (String s : arrayOfString) {
                if (s.length() == 1) {
                    str = str + "0" + s;
                } else {
                    str = str + s;
                }
            }
            return Long.parseLong(str);
        }
        return Long.parseLong(paramString);
    }

    public static String numDateToStr(Long paramLong) {
        if (paramLong == null) {
            return null;
        }
        String str = null;
        str = paramLong.toString().substring(0, 4) + "-" + paramLong.toString().substring(4, 6) + "-"
                + paramLong.toString().substring(6, 8);
        return str;
    }

    public static Date stringToDate(String paramString1, String paramString2) throws Exception {
        SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(paramString2);
        localSimpleDateFormat.setLenient(false);
        try {
            return localSimpleDateFormat.parse(paramString1);
        } catch (ParseException localParseException) {
            throw new Exception("解析日期字符串时出错！");
        }
    }

    public static String dateToString(Date paramDate, String paramString) {
        SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(paramString);
        localSimpleDateFormat.setLenient(false);
        return localSimpleDateFormat.format(paramDate);
    }

    public static Date compactStringToDate(String paramString) throws Exception {
        return stringToDate(paramString, "yyyyMMdd");
    }

    public static String dateToCompactString(Date paramDate) {
        return dateToString(paramDate, "yyyyMMdd");
    }

    public static String dateToNormalString(Date paramDate) {
        return dateToString(paramDate, "yyyy-MM-dd");
    }

    public static String dateToNormalMDString(Date paramDate) {
        return dateToString(paramDate, "MM/dd");
    }

    public static String dateToTString(Date paramDate) {
        return DateFormatUtils.format(paramDate, "yyyy-MM-dd") + "T" + DateFormatUtils.format(paramDate, "HHmmss");
    }

    public static String dateToMSecondString(Date paramDate) {
        return dateToString(paramDate, "yyyyMMddHHmmssSSS");
    }

    public static String compactStringDateToNormal(String paramString) throws Exception {
        return dateToNormalString(compactStringToDate(paramString));
    }

    public static int getDaysBetween(Date paramDate1, Date paramDate2) throws Exception {
        Calendar localCalendar1 = Calendar.getInstance();
        Calendar localCalendar2 = Calendar.getInstance();
        localCalendar1.setTime(paramDate1);
        localCalendar2.setTime(paramDate2);
        if (localCalendar1.after(localCalendar2)) {
            throw new Exception("起始日期小于终止日期!");
        }
        int i = localCalendar2.get(6) - localCalendar1.get(6);
        int j = localCalendar2.get(1);
        if (localCalendar1.get(1) != j) {
            localCalendar1 = (Calendar) localCalendar1.clone();
            do {
                i += localCalendar1.getActualMaximum(6);
                localCalendar1.add(1, 1);
            } while (localCalendar1.get(1) != j);
        }
        return i;
    }

    public static Date addDays(Date paramDate, int paramInt) {
        Calendar localCalendar = Calendar.getInstance();
        localCalendar.setTime(paramDate);
        int i = localCalendar.get(6);
        localCalendar.set(6, i + paramInt);
        return localCalendar.getTime();
    }

    public static Date addDays(String paramString1, String paramString2, int paramInt) throws Exception {
        Calendar localCalendar = Calendar.getInstance();
        Date localDate = stringToDate(paramString1, paramString2);
        localCalendar.setTime(localDate);
        int i = localCalendar.get(6);
        localCalendar.set(6, i + paramInt);
        return localCalendar.getTime();
    }

    public static Date addHours(Date paramDate, int paramInt) {
        Calendar localCalendar = Calendar.getInstance();
        localCalendar.add(Calendar.HOUR_OF_DAY, paramInt);
        return localCalendar.getTime();
    }

    public static java.sql.Date getSqlDate(Date paramDate) throws Exception {
        java.sql.Date localDate = new java.sql.Date(paramDate.getTime());
        return localDate;
    }

    public static String formatDate(Date paramDate, String parttern) {
        if (paramDate == null) {
            return null;
        }
        SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(parttern);
        localSimpleDateFormat.setLenient(false);
        return localSimpleDateFormat.format(paramDate);
    }

    public static String formatDate(Date paramDate) {
        if (paramDate == null) {
            return null;
        }
        localSimpleDateFormat.setLenient(false);
        return localSimpleDateFormat.format(paramDate);
    }

    public static String formatDateTime(Date paramDate) {
        if (paramDate == null) {
            return null;
        }
        SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        localSimpleDateFormat.setLenient(false);
        return localSimpleDateFormat.format(paramDate);
    }

    public static Date parse(String paramString, String pattern) throws Exception {
        if (paramString == null || paramString.trim().equals("")) {
            return null;
        }
        SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(pattern);
        localSimpleDateFormat.setLenient(false);
        try {
            return localSimpleDateFormat.parse(paramString);
        } catch (ParseException localParseException) {
            throw new Exception("日期解析出错！", localParseException);
        }
    }

    public static Date parseDate(String paramString) {
        if (paramString == null || paramString.trim().equals("")) {
            return null;
        }
        localSimpleDateFormat.setLenient(false);
        try {
            return localSimpleDateFormat.parse(paramString);
        } catch (ParseException localParseException) {
            log.error("===> parseDate error : ", localParseException);
        }
        return null;
    }

    public static Date parseDateTime(String paramString) throws Exception {
        if (paramString == null || paramString.trim().equals("")) {
            return null;
        }
        SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        localSimpleDateFormat.setLenient(false);
        try {
            return localSimpleDateFormat.parse(paramString);
        } catch (ParseException localParseException) {
            throw new Exception("时间解析异常！", localParseException);
        }
    }

    public static Integer getYM(String paramString) throws Exception {
        if (paramString == null) {
            return null;
        }
        localSimpleDateFormat.setLenient(false);
        Date localDate;
        try {
            localDate = localSimpleDateFormat.parse(paramString);
        } catch (ParseException localParseException) {
            throw new Exception("时间解析异常！", localParseException);
        }
        return getYM(localDate);
    }

    public static Integer getYM(Date paramDate) {
        if (paramDate == null) {
            return null;
        }
        Calendar localCalendar = Calendar.getInstance();
        localCalendar.setTime(paramDate);
        int i = localCalendar.get(1);
        int j = localCalendar.get(2) + 1;
        return new Integer(i * 100 + j);
    }

    public static int addMonths(int paramInt1, int paramInt2) {
        Calendar localCalendar = Calendar.getInstance();
        localCalendar.set(1, paramInt1 / 100);
        localCalendar.set(2, paramInt1 % 100 - 1);
        localCalendar.set(5, 1);
        localCalendar.add(2, paramInt2);
        return getYM(localCalendar.getTime()).intValue();
    }

    public static Date addMonths(Date paramDate, int paramInt) {
        Calendar localCalendar = Calendar.getInstance();
        localCalendar.setTime(paramDate);
        localCalendar.add(2, paramInt);
        return localCalendar.getTime();
    }

    public static int monthsBetween(int paramInt1, int paramInt2) {
        return (paramInt2 / 100 * 12 + paramInt2 % 100)
                - (paramInt1 / 100 * 12 + paramInt1 % 100);
    }

    public static int monthsBetween(Date paramDate1, Date paramDate2) {
        return monthsBetween(getYM(paramDate1).intValue(), getYM(paramDate2).intValue());
    }

    public static String getChineseDate(Calendar paramCalendar) {
        int i = paramCalendar.get(1);
        int j = paramCalendar.get(2);
        int k = paramCalendar.get(5);
        StringBuffer localStringBuffer = new StringBuffer();
        localStringBuffer.append(i);
        localStringBuffer.append("年");
        localStringBuffer.append(j + 1);
        localStringBuffer.append("月");
        localStringBuffer.append(k);
        localStringBuffer.append("日");
        return localStringBuffer.toString();
    }

    public static String getChineseWeekday(Calendar paramCalendar) {
        switch (paramCalendar.get(Calendar.DAY_OF_WEEK)) {
            case 2:
                return "星期一";
            case 3:
                return "星期二";
            case 4:
                return "星期三";
            case 5:
                return "星期四";
            case 6:
                return "星期五";
            case 7:
                return "星期六";
            case 1:
                return "星期日";
            default:
                return "未知";
        }
    }

    /**
     * 设置某天的23:59:59
     *
     * @param date
     * @return
     */
    public static Date setLastTimeInOneDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }

    /**
     * 设置某天的00:00:00
     *
     * @param date
     * @return
     */
    public static Date setFirstTimeInOneDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 00);
        cal.set(Calendar.MINUTE, 00);
        cal.set(Calendar.SECOND, 00);
        cal.set(Calendar.MILLISECOND, 00);
        return cal.getTime();
    }

    /**
     * date 转 calendar
     *
     * @param date
     * @return
     */
    public static Calendar date2Cal(Date date) {
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(parseDate(formatDate(date)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cal;
    }

    public static String getUniqueTimeString() {
        StringBuilder sb = new StringBuilder();
        sb.append(DateUtil.dateToString(new Date(), DateUtil.DETE_ALL_SIX));
        sb.append(String.valueOf((int) ((Math.random() * 9 + 1) * 100000)));

        return sb.toString();
    }

    /**
     * 随机指定范围内N个不重复的数 最简单最基本的方法
     *
     * @param min 指定范围最小值
     * @param max 指定范围最大值
     * @param n   随机数个数
     */
    public static int[] randomCommon(int min, int max, int n) {
        if (n > (max - min + 1) || max < min) {
            return null;
        }
        int[] result = new int[n];
        int count = 0;
        while (count < n) {
            int num = (int) (Math.random() * (max - min)) + min;
            boolean flag = true;
            for (int j = 0; j < n; j++) {
                if (num == result[j]) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                result[count] = num;
                count++;
            }
        }
        return result;
    }

    /**
     * 获取两个时间的时间天数
     */
    public static long getDatePoorDay(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        return day;
    }

    /**
     * 获取两个时间的时间查 如1天2小时30分钟
     */
    public static long getDatePoor(Date endDate, Date nowDate) {

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        long sec = diff % nd % nh % nm / ns;
        // day + "天" + hour + "小时" + min + "分钟"+ sec;
        return day * 24 * 60 + hour * 60 + min + (sec > 0 ? +1 : 0);

    }

    /**
     * 计算两个时间差多少秒
     * @param start
     * @param end
     * @return
     */
    public static long getDatePoorSecond(Date start, Date end) {
        if(start == null || end == null) {
            return 0;
        }
        long cs = start.getTime() - end.getTime();
        return cs > 0 ? cs / 1000 : 0;
    }

    /**
     * 计算两个日期主键分钟数
     *
     * @param endDate   截止日期
     * @param beginDate 开始日期
     * @return 间隔分钟数
     */
    public static long getTimeMinutesLength(Date endDate, Date beginDate) {
        if (endDate == null || beginDate == null) {
            return 0;
        }

        long len = endDate.getTime() - beginDate.getTime();
        // 1000 * 60 转换为分钟
        return len % 60000 == 0 ? len / 60000 : len / 60000 + 1;
    }

    public static boolean isInDate(Date date, Date beginDate, Date endDate) {
        if (date == null) {
            return false;
        }
        if (beginDate == null && endDate == null) {
            return false;
        }
        return (beginDate == null || date.after(beginDate))
                && (endDate == null || date.before(endDate));
    }

    public static boolean isSameDay(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            return false;
        }
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        if (cal1.get(Calendar.YEAR) != cal2.get(Calendar.YEAR)) {
            return false;
        }
        if (cal1.get(Calendar.MONTH) != cal2.get(Calendar.MONTH)) {
            return false;
        }
        if (cal1.get(Calendar.DAY_OF_MONTH) != cal2.get(Calendar.DAY_OF_MONTH)) {
            return false;
        }
        return true;
    }


    public static Date dayStartDate(Date date) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    public static Date dayEndDate(Date date) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 999);
        return c.getTime();
    }

    public static String getNow() {
        return getNow(DETE_ALL_SIX);
    }

    public static String getNow(String pattern) {
        return new SimpleDateFormat(pattern).format(new Date());
    }


    public static Long getIntervalSeconds(Date start, Integer intervalMin) {
        if (start == null) {
            return 0L;
        }
        long intervalSeconds = start.getTime() + intervalMin * 60 * 1000 - System.currentTimeMillis();
        return intervalSeconds > 0L ? intervalSeconds : 0L;
    }

    public static Long getIntervalSeconds(Date start, Date end) {
        if (start == null) {
            return 0L;
        }
        long intervalSeconds = start.getTime() - end.getTime();
        return intervalSeconds > 0L ? intervalSeconds : 0L;
    }

    /**
     * 获取指定分钟后的时候
     *
     * @param start 开始时间
     * @param min   多少分钟后
     * @return
     */
    public static Long getMinAfterTime(Date start, Integer min) {
        Long time = start.getTime() + (min * 60 * 1000);
        return time;
    }


    /**
     * 获取给定日期当前周的周一 00:00:00
     *
     * @param date
     * @return
     */
    public static Date getMondayStart(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 是否是工作时间
     * 周中 8:00 - 21:00 ，周末 9:00 - 18:00为工作时间
     *
     * @param date
     * @return 上班时间:True 非上班时间:FALSE
     */
    public static boolean isWorkTime(Date date) {
        if (date == null) {
            return false;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        if (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY) {  //周末
            return hour >= 9 && hour < 18;
        } else { //周中
            return hour >= 8 && hour < 21;
        }

    }

    /**
     * HH:mm:ss to HH:mm
     */
    public static String toHourMinute(String time) {
        if (StringUtils.isBlank(time)) {
            return null;
        }
        if (time.length() < 8) {
            time += ":00";
        }
        Time sqlTime = Time.valueOf(time);
        return new SimpleDateFormat("HH:mm").format(sqlTime);
    }

    /**
     * HH:mm:ss to HH:mm
     */
    public static String toHourMinute(Time time) {
        if (time == null) {
            return null;
        }
        return new SimpleDateFormat("HH:mm").format(time);
    }

    /**
     * 减少天数
     *
     * @param date
     * @param amount
     * @return
     */
    public static Date reduceDays(Date date, int amount) {
        if (null == date) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        if (amount < 0) {
            cal.add(Calendar.DATE, amount);
        } else {
            cal.add(Calendar.DATE, -amount);
        }
        return cal.getTime();
    }

    /**
     * 增加天数
     *
     * @param date
     * @param interval
     * @return
     */
    public static Date addDay(Date date, int interval) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, interval);
        return calendar.getTime();
    }

    public static List<Date> getDaysRage(Date startDate, Date endDate) {
        List<Date> dates = new ArrayList<>();
        DateTime date1 = new DateTime(startDate);
        DateTime date2 = new DateTime(endDate);
        int num = Days.daysBetween(date1, date2).getDays();
        if (num < 0) {
            return dates;
        }
        dates.add(startDate);
        while (num > 0) {
            date1 = date1.plusDays(1);
            dates.add(date1.toDate());
            num--;
        }
        return dates;
    }

    /**
     * date2比date1多的天数
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static int getIntervalDays(Date startDate, Date endDate) {
        return 1 + Days.daysBetween(new DateTime(DateUtil.formatDate(startDate, DateUtil.NORMAL_DATE_FORMAT)), new DateTime(DateUtil.formatDate(endDate, DateUtil.NORMAL_DATE_FORMAT))).getDays();
    }


    public static String getZhHour(Date paramDate) {
        DateTime dateTime = new DateTime(paramDate);
        Integer hour = dateTime.getHourOfDay();
        if (hour > 12) {
            return "下午" + (hour - 12) + "点";
        } else {
            return "上午" + hour + "点";
        }
    }

    /**
     * 获取endDate与startDate日期相隔天数
     *
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @return
     */
    public static Long differentDays(Date startDate, Date endDate) {
        if (startDate == null || endDate == null) {
            return 0L;
        }
        return (endDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000);
    }

    /**
     * 获取endDate与startDate日期之间的日期列表，按pattern格式化
     *
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @param pattern   格式化规则
     * @return
     */
    public static List<String> differentDaysStr(Date startDate, Date endDate, String pattern) {
        List<String> daysList = new ArrayList<>(Arrays.asList(formatDate(startDate, pattern)));
        long days = differentDays(startDate, endDate);
        for (int i = 1; i < days + 1; i++) {
            daysList.add(formatDate(addDay(startDate, i), pattern));
        }
        return daysList;
    }


    /**
     * 判断传入的时间是否与当前时间的年月日相等
     * @param target
     * @return
     */
    public static Boolean isEquals(Date target) {
        String targetStr = dateToString(target, COMPACT_DATE_FORMAT);
        String nowStr = dateToString(new Date(), COMPACT_DATE_FORMAT);
        return targetStr.equals(nowStr);
    }

    private static String getTimeInterval() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        TimeZone time1 = TimeZone.getTimeZone("America/Los_Angeles");
        Calendar c = Calendar.getInstance(time1, Locale.US);

        Calendar cal = Calendar.getInstance();
        cal.setTime(c.getTime());
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // System.out.println("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期
        // 设置一个星期的第一天
        cal.setFirstDayOfWeek(Calendar.SUNDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        String imptimeBegin = sdf.format(cal.getTime());
//        System.out.println("所在周星期一的日期：" + imptimeBegin);
        cal.add(Calendar.DATE, 6);
        String imptimeEnd = sdf.format(cal.getTime());
//        System.out.println("所在周星期日的日期：" + imptimeEnd);
        return imptimeBegin + "," + imptimeEnd;
    }

    public static List<Date> findDates(Date dBegin, Date dEnd){
        List lDate = new ArrayList();
        lDate.add(dBegin);
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(dEnd);
        // 测试此日期是否在指定日期之后
        while (dEnd.after(calBegin.getTime()))
        {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            lDate.add(calBegin.getTime());
        }
        return lDate;
    }

    /**
     * 根据传入的年月日返回日的天数，（主要是为了处理平年、闰年 2月的28天和29天的情况）
     * @param year
     * @param month
     * @param day
     * @return
     */
    public static int getDay(int year, int month, int day) {
        switch (month) {
            case 2:
                day = Math.min(day, IsoChronology.INSTANCE.isLeapYear(year) ? 29 : 28);
                break;
        }
        return day;
    }


    public static void main(String[] args) throws Exception {
//        List<OnWeekDay> weekDateList = getWeekDateList();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
////        LocalDate inputDate = LocalDate.parse();
//// 计算周开始时间 = 当前日期 - （当前日期是周几 - 周一）的日期差
//        TemporalAdjuster FIRST_OF_WEEK = TemporalAdjusters.ofDateAdjuster(localDate -> localDate.minusDays(localDate.getDayOfWeek().getValue()-DayOfWeek.MONDAY.getValue()));
//        System.out.println(inputDate.with(FIRST_OF_WEEK));
//
//// 计算周开始时间 = 当前日期 + （周日 - 当前日期是周几）的日期差
//        TemporalAdjuster LAST_OF_WEEK = TemporalAdjusters.ofDateAdjuster(localDate -> localDate.plusDays(DayOfWeek.SUNDAY.getValue() - localDate.getDayOfWeek().getValue()));
//        System.out.println(inputDate.with(LAST_OF_WEEK));

    }


}
