package com.gyee.frame.util;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 日期处理
 * @ClassName: DateUtils
 * @author gyee
 * @date 2019-06-23 00:53
 * @version V1.0
 */
public class DateUtils  extends org.apache.commons.lang3.time.DateUtils {
    /**
     * 时间格式(yyyy-MM-dd)
     */
    public final static String DATE_PATTERN = "yyyy-MM-dd";
    /**
     * 时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static String format(Date date) {
        return format(date, DATE_PATTERN);
    }

    public static String formatDateTime(Date date) {
        return format(date, DATE_TIME_PATTERN);
    }
    /**
     * 时间转换
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String format(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }

    /**
     * 时间转换
     *
     * @param time
     * @param pattern
     * @return
     */
    public static String format(Long time, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date = new Date(time);
        String format = sdf.format(date);
        return format;
    }


    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

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

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

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

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate() {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate() {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime() {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow() {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format) {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date) {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts) {
        try {
            return new SimpleDateFormat(format).parse(ts);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String 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;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    /**
     * 将时间的时分秒毫秒字段去掉
     *
     * @param date
     * @return
     */
    public static Date truncate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 计算两个时间之间差的天数(取整后)
     *
     * @param d1
     * @param d2
     * @return
     */
    public static int daysDiff(Date d1, Date d2) {
        return (int) Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (60 * 60 * 24 * 1000));
    }

    /**
     * 将字符串日期转换成日期类型
     *
     * @param time
     * @return
     */
    public static Date parseStrtoDate(String time) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dateTime = null;
        try {
            dateTime = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();

        }
        return dateTime;
    }

    public static Date parseStrtoDate(String time,String pattern) {

        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date dateTime = null;
        try {
            dateTime = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();

        }
        return dateTime;
    }

    public static double hoursDiff1(Date d1, Date d2) {
        return Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (double) (60 * 60 * 1000));
    }

    public static double hoursDiff2(Date d1, Date d2) {
        return Math.abs((d1.getTime() - d2.getTime())) / (double) (60 * 60 * 1000);
    }

    /**
     * 计算两个时间之间差的分钟数(取整后)
     *
     * @param d1
     * @param d2
     * @return
     */
    public static int minutesDiff(Date d1, Date d2) {
        return (int) Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (60 * 1000));
    }

    /**
     * 计算两个时间之间差的分钟数(取整后)
     *
     * @param d1
     * @param d2
     * @return
     */
    public static double minutesDiff2(Date d1, Date d2) {
        return Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (60 * 1000));
    }

    /**
     * 获取系统时间
     *
     * @return
     */
    public static Date now() {
        return new Date();
    }

    /**
     * 获取当前月的第一天
     *
     * @return
     */
    public static String getCurrtenFirstDay() {

        Calendar c = Calendar.getInstance();
        // c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);
        return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
    }

    /**
     * 获取当前月的最后一天
     *
     * @return
     */
    public static String getCurrtenLastDay() {

        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        return new SimpleDateFormat("yyyy-MM-dd").format(ca.getTime());
    }
    /**
     * 计算两个时间之间差的小时数(取整后)
     *
     * @param d1
     * @param d2
     * @return
     */
    public static int hoursDiff(Date d1, Date d2) {
        return (int) Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (60 * 60 * 1000));
    }

    /**
     * 获取当前整点数
     *
     * @return
     */
    public static Date getCurrHourTime(Date date) {
        Calendar ca = Calendar.getInstance();
        int minute = ca.get(Calendar.MINUTE);
        if (minute < 30) {
            ca.set(Calendar.MINUTE, 30);
        } else {
            ca.add(Calendar.HOUR, 1);
            ca.set(Calendar.MINUTE, 0);
        }

        ca.set(Calendar.SECOND, 5);
        date = ca.getTime();

        return date;
    }

    //普通时间转为UTC
    public static String localToUTC(String localTimeStr) {
        try {
            Date localDate = getLocalSDF().parse(localTimeStr);
            return getUTCSDF().format(localDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    //UTC转为普通时间
    public static String utcToLocal(String utcTimeStr) {
        try {
            Date date = getUTCSDF().parse(utcTimeStr);
            return getLocalSDF().format(date);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static SimpleDateFormat getLocalSDF() {
        return new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
    }

    private static SimpleDateFormat getUTCSDF() {
        SimpleDateFormat utcSDF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        utcSDF.setTimeZone(TimeZone.getTimeZone("UTC"));
        return utcSDF;
    }

}