package com.bonus.boot.manager.manager.utils;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 时间操作类
 *
 * @author cshu
 */
@SuppressWarnings("deprecation")
public class DateTimeHelper {

    /**
     * 获取当前时间的年份
     *
     * @return
     */
    public static String getNowYear() {
        return format(new Date(), "yyyy");
    }

    public static String getYMDToYMDHMS(String time) {
        String ymdhms = time+ " 00:00:01";
        return ymdhms;
    }
    public static String getYMDToYMDHMS2(String time) {
        String ymdhms = time+ " 23:59:59";
        return ymdhms;
    }
    /**
     * 获取当前月份
     *
     * @return
     */
    public static String getNowMonths() {
        return format(new Date(), "MM");
    }

    public static String getYear(Date d) {
        int year = d.getYear() + 1900;
        return year + "";
    }

    public static String getMonth(Date d) {
        int month = d.getMonth() + 1;
        return StringHelper.fillPrefixZero(month, 2);
    }

    public static String getDay() {
        return format(new Date(),"dd");
    }
    /**
     * 格式化 时间
     *
     * @param format
     * @return
     */
    public static String format(Date d, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(d);
    }

    public static String format(String str, String format, String disFormat) {
        Date d = parse(str, format);
        return format(d, disFormat);
    }

    /**
     * 格式化 时间
     *
     * @param format
     * @return
     */
    public static String format(Date d, DateFormat format) {
        return format.format(d);
    }

    /**
     * 格式化 时间
     *
     * @param format
     * @return Date
     * @throws ParseException
     */
    public static Date parse(String dateStr, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        try {
            return df.parse(dateStr);
        } catch (ParseException e) {
            return new Date();
        }
    }

    public static Date parse(String dateStr, String format, Date d) {
        try {
            SimpleDateFormat df = new SimpleDateFormat(format);
            return df.parse(dateStr);
        } catch (Exception e) {
            return d;
        }
    }

    /**
     * 解析时间
     *
     * @return Date
     * @throws ParseException
     */
    public static Date parse(String dateStr, DateFormat format)
            throws ParseException {
        return format.parse(dateStr);
    }

    /**
     * 获取当前时间的上个年月
     *
     * @return
     */
    public static String getPrevMonth() {
        return minusMonth(new Date(), 1);
    }

    /**
     * 获取当前时间的年月
     *
     * @return
     */
    public static String getNowMonth() {
        return format(new Date(), "yyyy-MM");
    }

    public static String getNowDay() {
        return format(new Date(),"dd");
    }

    /**
     * 当前时间的上一年的年月日
     *
     * @return
     */
    public static String getPrevDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();


        c.setTime(new Date());
        c.add(Calendar.YEAR, -1);
        Date y = c.getTime();
        String year = format.format(y);
        return year;

    }

    /**
     * 获取当前时间的年月日
     *
     * @return
     */
    public static String getNowDate() {
        return format(new Date(), "yyyy-MM-dd");
    }

    /**
     * 获取当前为本年的第几周
     *
     * @return
     */
    public static String getNowWeek() {
        Calendar ca = Calendar.getInstance();
        ca.setTime(new Date());
        int week = ca.get(Calendar.WEEK_OF_YEAR);
        return format(new Date(), "yyyy") + "-" + week;
    }

    public static String getNowDayHMS() {
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        return time;
    }

    /**
     * @return
     */
    public static String minusMonth(Date date, int c) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.MONTH, -c);
            return format(cal.getTime(), df);
        } catch (Exception e) {
            return format(date, df);
        }
    }

    /**
     * @param month
     * @return
     */
    public static String minusMonth(String month, int c) {
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
            Date date = parse(month, df);

            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.MONTH, -c);

            return format(cal.getTime(), df);
        } catch (Exception e) {
            return month;
        }
    }

    /**
     * @return
     */
    public static Date minus(Date d, int type, int c) {
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(d);
            cal.add(type, -c);
            return cal.getTime();
        } catch (Exception e) {
            return new Date();
        }
    }

    /**
     * 得到前一个月
     *
     * @param month
     * @return
     */
    public static String prevMonth(String month) {
        return minusMonth(month, 1);
    }

    /**
     * 得到前一个月
     *
     * @param month
     * @return
     */
    public static String prevMonth(String month, int c) {
        return minusMonth(month, c);
    }

    public static float duration(String startTime, String stopTime) {
        Date sd = DateTimeHelper.parse(startTime, "yyyy-MM-dd HH:mm");
        long sl = sd.getTime();

        Date ed = DateTimeHelper.parse(stopTime, "yyyy-MM-dd HH:mm");
        long el = ed.getTime();

        float l = el - sl;
        if (l <= 0) {
            l = 0;
        }
        l = l / (1000 * 60);
        return l;
    }

    public static String getStartYearMonthBySeason(String year, int season) {
        switch (season) {
            case 1:
                return getYearMonth(year, "01");
            case 2:
                return getYearMonth(year, "04");
            case 3:
                return getYearMonth(year, "07");
            case 4:
                return getYearMonth(year, "10");
            default:
                return getYearMonth(year, "01");
        }
    }

    private static String getYearMonth(String year, String month) {
        return year + "-" + month;
    }

    public static String getEndYearMonthBySeason(String year, int season) {
        switch (season) {
            case 1:
                return getYearMonth(year, "03");
            case 2:
                return getYearMonth(year, "06");
            case 3:
                return getYearMonth(year, "09");
            case 4:
                return getYearMonth(year, "12");
            default:
                return getYearMonth(year, "03");
        }
    }

    /**
     * 获取当年初始时间
     *
     * @return
     */
    public static String getYearStart() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        return format.format(calendar.getTime());
    }

    /**
     * 获取当前日期周一
     *
     * @return
     */
    public static String getMonday() {
        Calendar cal = Calendar.getInstance(Locale.CHINA);
//      如果当前时间是星期天，则向上移动一天，再取本周的星期一，老外用周日到周六为一周，向前移动一天，则是中国人的本周  
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            cal.add(Calendar.DATE, -1);
        }
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        return sdf1.format(cal.getTime());

    }

    /**
     * 获取当前时间的年月
     *
     * @return
     */
    public static String getYmDate() {
        return format(new Date(), "yyyy-MM");
    }


    /**
     * 将时间转换为时间戳
     */
    public static String dateToStamp(String time) {
        String stap;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        long ts = date.getTime() / 1000;//获取时间的时间戳
        stap = String.valueOf(ts);
        return stap;
    }

    /**
     * 将时间戳转换为时间
     */
    public static String stampToDate(String stap) {
        String time;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(stap);
        Date date = new Date(lt * 1000);
        time = simpleDateFormat.format(date);
        return time;
    }



    public static List<String> findDates(String stime, String etime)
            throws ParseException {
        List<String> allDate = new ArrayList();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Date dBegin = sdf.parse(stime);
        Date dEnd = sdf.parse(etime);
        allDate.add(sdf.format(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);
            allDate.add(sdf.format(calBegin.getTime()));
        }
        return allDate;
    }

    public static String  getDate(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dates = DateTimeHelper.parse(date, sdf);
        return DateTimeHelper.format(dates, "yyyy-MM-dd");
    }

    public static int dateCompare(Date date1, Date date2) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String dateFirst = dateFormat.format(date1);
        String dateLast = dateFormat.format(date2);
        int dateFirstIntVal = Integer.parseInt(dateFirst);
        int dateLastIntVal = Integer.parseInt(dateLast);
        if (dateFirstIntVal > dateLastIntVal) {
            return 1;
        } else if (dateFirstIntVal < dateLastIntVal) {
            return -1;
        }else if (dateFirstIntVal == dateLastIntVal){
            return 2;
        }
        return 0;
    }

    /**
     * 传入两个时间参数，返回需要的值
     * @param date1
     * @param date2
     * @return
     */
    public static int dateCompare(String date1, String date2){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date parse1 = sdf.parse(date1);
            Date parse2 = sdf.parse(date2);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
            String dateFirst = dateFormat.format(parse1);
            String dateLast = dateFormat.format(parse2);
            int dateFirstIntVal = Integer.parseInt(dateFirst);
            int dateLastIntVal = Integer.parseInt(dateLast);
            if (dateFirstIntVal > dateLastIntVal) {
                return 1;
            } else if (dateFirstIntVal < dateLastIntVal) {
                return -1;
            } else {
                return 0;
            }
        }catch (ParseException e){
            //假设转换错误，按照第一个时间来
            return 1;
        }
    }

    /**
     * 传入两个时间参数，返回需要的值
     * @param date1
     * @param date2
     * @param i>0 true 返回大的 ，false返回小的
     * @return
     */
    public static String getNeedDate(String date1, String date2,int i){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date parse1 = sdf.parse(date1);
            Date parse2 = sdf.parse(date2);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
            String dateFirst = dateFormat.format(parse1);
            String dateLast = dateFormat.format(parse2);
            int dateFirstIntVal = Integer.parseInt(dateFirst);
            int dateLastIntVal = Integer.parseInt(dateLast);
            if (dateFirstIntVal > dateLastIntVal && i > 0) {
                return date1;
            } else if (dateFirstIntVal < dateLastIntVal && i>0) {
                return date2;
            } else if (dateFirstIntVal < dateLastIntVal && i<0) {
                return date1;
            } else if (dateFirstIntVal > dateLastIntVal && i < 0) {
                return date2;
            } else {
                return date1;
            }
        }catch (ParseException e){
            //假设转换错误，按照第一个时间来
            return date1;
        }
    }

    /**
     * 计算两个时间点之间的天数
     */
    public static long getBetweenDay(LocalDate start, LocalDate end) {
        return end.toEpochDay() - start.toEpochDay();
    }

    /**
     * 计算两个时间点之间的天数
     */
    public static long getBetweenDay(String start, String end) {
        LocalDate date1 = LocalDate.parse(start, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate date2 = LocalDate.parse(end, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        return date2.toEpochDay() - date1.toEpochDay();
    }


    public static String getNowTime() {
        return format(new Date(), "yyyy-MM-dd HH:mm:ss");
    }

    public static String getNowTime2() {
        return format(new Date(), "yyyyMMddHHmmss");
    }
    public static String getNowTime3() {
        return format(new Date(), "yyyyMMdd");
    }

    /**
     * 获取当前时间毫秒
     * @return
     */
    public static String getNowTimestamp() {

        return String.valueOf(System.currentTimeMillis());

    }

    /**
     * 判断当前日期是否为周末
     * @param
     * @return
     */
    public static boolean isWeekend() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date()); // 设置日期对象
        int weekday = calendar.get(Calendar.DAY_OF_WEEK); // 获取星期几信息
        // 判断是否为周六或周日
        if (weekday == Calendar.SUNDAY) {
            return true;
        } else {
            return false;
        }
    }

}
