package com.sunwin.robot2ips.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期时间工具方法
 * <p>
 * Created by shiyanfei on 2018-01-09.
 */
public class DateUtil {

    /**
     * 格式 2015-08-07
     */
    //public static final String DateFormat_yyyyMMdd = "yyyy-MM-dd";
    /**
     * 格式 15:25:30
     */
    public static final String DateFormat_HHmmss = "HH:mm:ss";

    /**
     * 格式 15:25
     */
    public static final String DateFormat_HHmm = "HH:mm";

    /**
     * 格式 2015-08
     */
    public static final String DateFormat_yyyyMM = "yyyy-MM";

    /**
     * 格式 2015-08
     */
    public static final String DateFormat_yyyy_MM_dd = "yyyy-MM-dd";

    /**
     * 格式 2015-08-05 23
     */
    public static final String DateFormat_yyyy_MM_dd_HH = "yyyy-MM-dd HH";

    /**
     * 格式 2015-08-07 11:44:18
     */
    public static final String DateFormat_yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";

    /**
     * 格式 2015-08-07 11:44:18:591
     */
    public static final String DateFormat_yyyyMMddHHmmssSSS = "yyyy-MM-dd HH:mm:ss.SSS";

    /**
     * 格式 20200115
     */
    public static final String DateFormat_yyyyMMdd = "yyyyMMdd";

    /**
     * 格式 150807114418
     */
    public static final String DateFormat_yyMMddHHmmss = "yyMMddHHmmss";

    /**
     * 格式 20140416142030
     */
    public static final String DateFormat_TimeStemp_yyyyMMddHHmmss = "yyyyMMddHHmmss";

    /**
     * 格式 142030
     */
    public static final String DateFormat_TimeStemp_HHmmss = "HHmmss";

    /**
     * 格式 20150807114418591
     */
    public static final String DateFormat_TimeStemp = "yyyyMMddHHmmssSSS";

    /**
     * 格式 2015-08-13T16:49:02.039+0800
     */
    public static final String DateFormat_yyyyMMddTHHmmssSSSZ = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";

    /**
     * 格式 2015-11-09T00:00:08.000+08:00
     */
    public static final String DateFormat_yyyyMMddTHHmmssSSSXXX = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";


    /**
     * 日期转化为字符串
     *
     * @param format
     * @return
     */
    public static String parseToString(Date date, String format) {
        if (date != null) {
            DateFormat df = new SimpleDateFormat(format);
            String strDate = df.format(date);
            return strDate;
        } else {
            return "-";
        }
    }

    /**
     * 格式化日期
     */
    public static Date getFormatDate(Date date, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            String strDate = sdf.format(date);
            Date newDate = sdf.parse(strDate);
            return newDate;
        } catch (Exception e) {
            return null;
        }
    }

    public static long compareDate(Date date1, Date date2) {
        long l = date2.getTime() - date1.getTime();
        return l / (1000 * 60);
    }

    /**
     * 将字符串转为时间戳
     *
     * @param time
     * @return
     */
    public static Long getTime(String time) {
        try {
            String re_time = null;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date d = sdf.parse(time);
            long l = d.getTime();
            re_time = String.valueOf(l);
            return Long.parseLong(re_time);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 将时间戳转为字符串
     *
     * @param time
     * @return
     */
    public static String getStrTime(String time) {
        String re_StrTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lcc_time = Long.valueOf(time);
        re_StrTime = sdf.format(new Date(lcc_time));
        return re_StrTime;
    }

    /**
     * 将时间戳转为字符串
     *
     * @param time
     * @return
     */
    public static String getStrDate(Long time, String format) {
        String re_StrTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        long lcc_time = Long.valueOf(time);
        re_StrTime = sdf.format(new Date(lcc_time));
        return re_StrTime;
    }

    /**
     * 将时间戳转为字符串
     *
     * @param time
     * @return
     */
    public static Date getLongDate(Long time, String format) {

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        long lcc_time = Long.valueOf(time);
        String re_StrTime = sdf.format(new Date(lcc_time));

        Date parse = null;
        try {
            parse = sdf.parse(re_StrTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    /**
     * String转date
     *
     * @param dateStr
     * @return
     */
    public static Date strToDate(String dateStr) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.parse(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @Description: string 转 date
     * @Param: [dateStr, format]
     * @Author: shiwn
     * @Date: 2021/1/27 14:47
     */
    public static Date strToDate(String dateStr, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @Description: 获取某年某月有多少天
     * @Param: [year：年份, month：月份]
     * @Author: shiwn
     * @Date: 2020/1/20 11:09
     */
    public static int getDayByYearMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, 0);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * @Description: 根据输入的参数生成固定时间
     * @Param: [year：年份, month：月份, day：日期, time：时间]
     * @Author: shiwn
     * @Date: 2020/1/20 15:48
     */
    public static Date buildDate(String year, String month, String day, String time) {
        if (time == null) {
            time = "00:00:01";
        }
        if (day == null) {
            day = "01";
        }
        //  判断日期是否满足条件
        int maxDay = getMaxDayOfMonth(Integer.parseInt(year), Integer.parseInt(month));
        if (maxDay < Integer.parseInt(day)) {
            return null;
        }
        try {
            day = formatDay(day);
            String dateStr = year + "-" + month + "-" + day + " " + time;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(dateStr);
            return date;
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * @Description: 获取每月有多少天
     * @Param: [year：年份, month：月份]
     * @Author: shiwn
     * @Date: 2020/1/22 11:43
     */
    public static int getMaxDayOfMonth(int year, int month) {
        //  获取每月有多少天
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        //  java月份从0开始
        calendar.set(Calendar.MONTH, month - 1);
        return calendar.getActualMaximum(Calendar.DATE);
    }

    /**
     * @Description: 获取该月有几周
     * @Param: [year：年份, month：月份]
     * @Author: shiwn
     * @Date: 2020/1/22 10:49
     */
    public static int getMonthWeek(int year, int month) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        //  java月份从0开始
        c.set(Calendar.MONTH, month - 1);
        return c.getActualMaximum(Calendar.WEEK_OF_MONTH);
    }

    /**
     * @Description: 返回DayOfWeek格式的星期几
     * @Param: [weekDay：星期几]
     * @Author: shiwn
     * @Date: 2020/1/22 11:13
     */
    public static DayOfWeek getWeekDay(int weekDay) {
        switch (weekDay) {
            case 0:
                return DayOfWeek.SUNDAY;
            case 1:
                return DayOfWeek.MONDAY;
            case 2:
                return DayOfWeek.TUESDAY;
            case 3:
                return DayOfWeek.WEDNESDAY;
            case 4:
                return DayOfWeek.THURSDAY;
            case 5:
                return DayOfWeek.FRIDAY;
            case 6:
                return DayOfWeek.SATURDAY;
            default:
                return null;
        }
    }


    /**
     * @Description: 日期的天数加减计算
     * @Param: [date：时间, days：间隔天数]
     * @Author: shiwn
     * @Date: 2021/1/21 15:24
     */
    public static Date getDateByDays(Date date, Integer days) {
        Calendar calendar = Calendar.getInstance();
        //设置起时间
        calendar.setTime(date);
        calendar.add(Calendar.DATE, days);
        return calendar.getTime();
    }

    /**
     * @Description: 计算两个时间点的间隔天数
     * @Param: [start, end]
     * @Author: shiwn
     * @Date: 2021/1/25 14:21
     */
    public static Integer getDayInterval(Date start, Date end) {
        final long nd = 1000 * 24 * 60 * 60;
        Date startDay = new Date(start.getTime() - start.getTime() % nd);
        Date endDay = new Date(end.getTime() - end.getTime() % nd);
        return (int) ((endDay.getTime() - startDay.getTime()) / nd);
    }

    /**
     * @Description: 计算两个时间点的间隔秒数
     * @Param: [start, end]
     * @Author: shiwn
     * @Date: 2021/1/25 15:54
     */
    public static Integer getSecondInterval(Date start, Date end) {
        final long nd = 1000;
        long diff = end.getTime() - start.getTime();
        return (int) (diff / nd);
    }

    /**
     * @Description: 计算两个时间点的间隔分钟数
     * @Param: [start, end]
     * @Author: shiwn
     * @Date: 2021/1/25 15:54
     */
    public static Integer getMinuteInterval(Date start, Date end) {
        final Float nd = 60000F;
        Float diff = Float.valueOf(end.getTime() - start.getTime());
        return (int) Math.ceil(diff / nd);
    }


    /**
     * @Description: 获取本周的开始日期
     * @Author: shiwn
     * @Date: 2021/1/28 10:35
     */
    public static Date getBeginDateOfWeek() {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 1) {
            dayOfWeek += 7;
        }
        calendar.add(Calendar.DATE, 2 - dayOfWeek);
        return calendar.getTime();
    }

    /**
     * @Description: 获取本周的结束日期
     * @Author: shiwn
     * @Date: 2021/1/28 10:53
     */
    public static Date getEndDateOfWeek() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getBeginDateOfWeek());
        calendar.add(Calendar.DAY_OF_WEEK, 6);
        return calendar.getTime();
    }

    /**
     * @Description: 将 字符串格式的时间戳 转为 时间格式的字符串
     * @Param: [timeStamp]
     * @Author: shiwn
     * @Date: 2021/1/14 17:16
     */
    public static String getStringByTimeStamp(String timeStamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
        return sd;
    }

    /**
     * @description: 当前日期年数加减计算
     * @author machao
     * @date: 2021/5/14 10:00
     */
    public static String getYearTime(Integer year) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        //过去一年
        c.setTime(new Date());
        c.add(Calendar.YEAR, year);
        Date y = c.getTime();
        return format.format(y);
    }

    /**
     * @Description: 获取昨天的开始时间
     * @Date: 2021/12/6 10:41
     * @Author: shiwn
     * @Return:
     */
    public static Date getYesterdayStartTime() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 1);
        c.add(Calendar.DATE, -1);
        return c.getTime();
    }

    /**
     * @Description: 获取昨天的结束时间
     * @Date: 2021/12/6 10:41
     * @Author: shiwn
     * @Return:
     */
    public static Date getYesterdayEndTime() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 999);
        c.add(Calendar.DATE, -1);
        return c.getTime();
    }

    /**
     * @Description: 获取上月的开始时间
     * @Date: 2021/12/6 10:43
     * @Author: shiwn
     * @Return:
     */
    public static Date getLastMonthdayStartTime() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -1);
        c.set(Calendar.DATE, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 1);
        return c.getTime();
    }

    /**
     * @Description: 获取上月的开始时间
     * @Date: 2021/12/6 11:12
     * @Author: shiwn
     * @Return:
     */
    public static Date getLastMonthdayEndTime() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DATE, 1);
        c.add(Calendar.MONTH, -1);
        //  取上月结束日期
        int lastMonthMaxDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        //  设置
        c.set(Calendar.DATE, lastMonthMaxDay);
        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();
    }

    // ———————————————————————— 以下为私有方法  ————————————————————————

    /**
     * @Description: 日期格式化，如：1转为01
     * @Param: [day：日期]
     * @Author: shiwn
     * @Date: 2020/1/20 15:53
     */
    private static String formatDay(String day) {
        if (day.length() == 1) {
            day = "0" + day;
        }
        return day;
    }
}
