package com.huashe.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

/**
 * 时间工具类
 *
 * @author ruoyi
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils
{
    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 final Logger log = LoggerFactory.getLogger(DateUtils.class);

    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);
    }

    /**
     * 计算时间差
     *
     * @param endDate 最后时间
     * @param startTime 开始时间
     * @return 时间差（天/小时/分钟）
     */
    public static String timeDistance(Date endDate, Date startTime)
    {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - startTime.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 + "分钟";
    }

    /**
     * 增加 LocalDateTime ==> Date
     */
    public static Date toDate(LocalDateTime temporalAccessor)
    {
        ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }

    /**
     * 增加 LocalDate ==> Date
     */
    public static Date toDate(LocalDate temporalAccessor)
    {
        LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
        ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }

    /**
     * $ 时间年偏移 <功能详细描述>
     *
     * @param date 日期
     * @param offset 偏移量
     * @return date 日期
     * @see [类、类#方法、类#成员]
     */
    public static Date adjustYear(Date date, int offset) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, offset);
        Date newDate = calendar.getTime();
        return newDate;
    }

    /**
     * $时间年偏移 <功能详细描述>
     *
     * @param time yyyyMMddHHmmss
     * @param offset 偏移量
     * @return yyyyMMddHHmmss
     * @see [类、类#方法、类#成员]
     */
    public static String adjustYear(String time, int offset) {
        return adjustYear(time, offset, YYYYMMDDHHMMSS);
    }

    /**
     * $时间年偏移 <功能详细描述>
     *
     * @param time yyyyMMddHHmmss
     * @param offset 偏移量
     * @param format 格式化
     * @return yyyyMMddHHmmss
     * @see [类、类#方法、类#成员]
     */
    public static String adjustYear(String time, int offset, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String retTime = null;

        try {
            Date date = formatter.parse(time);
            Date newDate = adjustYear(date, offset);
            retTime = formatter.format(newDate);
        }
        catch (ParseException e) {
            log.error("adjustYear fail!" + ExceptionUtils.getStackTrace(e));
        }

        return retTime;
    }

    /**
     * 时间月偏移
     * @param date 日期
     * @param offset 偏移量
     * @return date 日期
     */
    public static Date adjustMonth(Date date, int offset) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, offset);
        Date newDate = calendar.getTime();
        return newDate;
    }

    /**
     * $时间月偏移 <功能详细描述>
     *
     * @param time yyyyMMddHHmmss
     * @param offset 偏移量
     * @return yyyyMMddHHmmss
     * @see [类、类#方法、类#成员]
     */
    public static String adjustMonth(String time, int offset) {
        return adjustMonth(time, offset, YYYYMMDDHHMMSS);
    }

    /**
     * $时间月偏移 <功能详细描述>
     *
     * @param time yyyyMMddHHmmss
     * @param offset 偏移量
     * @param format 格式化时间
     * @return yyyyMMddHHmmss
     * @see [类、类#方法、类#成员]
     */
    public static String adjustMonth(String time, int offset, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String retTime = null;

        try {
            Date date = formatter.parse(time);
            Date newDate = adjustMonth(date, offset);
            retTime = formatter.format(newDate);
        }
        catch (ParseException e) {
            log.error("adjustMonth fail!" + ExceptionUtils.getStackTrace(e));
        }

        return retTime;
    }

    /**
     * $时间周偏移 <功能详细描述>
     *
     * @param date 日期
     * @param offset 偏移量
     * @return date
     * @see [类、类#方法、类#成员]
     */
    public static Date adjustWeek(Date date, int offset) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 7 * offset);
        Date newDate = calendar.getTime();
        return newDate;
    }

    /**
     * $时间周偏移 <功能详细描述>
     *
     * @param time yyyyMMddHHmmss
     * @param offset 偏移量
     * @return yyyyMMddHHmmss
     * @see [类、类#方法、类#成员]
     */
    public static String adjustWeek(String time, int offset) {
        return adjustWeek(time, offset, YYYYMMDDHHMMSS);
    }

    /**
     * $时间周偏移 <功能详细描述>
     *
     * @param time 字符串日期
     * @param offset 偏移量
     * @return yyyyMMddHHmmss
     * @see [类、类#方法、类#成员]
     */
    public static String adjustWeek(String time, int offset, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String retTime = null;

        try {
            Date date = formatter.parse(time);
            Date newDate = adjustWeek(date, offset);
            retTime = formatter.format(newDate);
        }
        catch (ParseException e) {
            log.error("adjustWeek fail!" + ExceptionUtils.getStackTrace(e));
        }

        return retTime;
    }

    /**
     * $时间日偏移 <功能详细描述>
     *
     * @param date 日期
     * @param offset 偏移量
     * @return yyyyMMddHHmmss
     * @see [类、类#方法、类#成员]
     */
    public static Date adjustDay(Date date, int offset) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, offset);
        Date newDate = calendar.getTime();
        return newDate;
    }

    /**
     * $时间日偏移 <功能详细描述>
     *
     * @param time yyyyMMddHHmmss
     * @param offset 偏移量
     * @return yyyyMMddHHmmss
     * @see [类、类#方法、类#成员]
     */
    public static String adjustDay(String time, int offset) {
        return adjustDay(time, offset, YYYYMMDDHHMMSS);
    }

    /**
     * $时间日偏移 <功能详细描述>
     *
     * @param time 字符串日期
     * @param offset 偏移量
     * @return yyyyMMddHHmmss
     * @see [类、类#方法、类#成员]
     */
    public static String adjustDay(String time, int offset, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String retTime = null;

        try {
            Date date = formatter.parse(time);
            Date newDate = adjustDay(date, offset);
            retTime = formatter.format(newDate);
        }
        catch (ParseException e) {
            log.error("adjustDay fail!" + ExceptionUtils.getStackTrace(e));
        }

        return retTime;
    }

    /**
     * $时间小时偏移 <功能详细描述>
     *
     * @param date 日期
     * @param offset 偏移量
     * @return yyyyMMddHHmmss
     * @see [类、类#方法、类#成员]
     */
    public static Date adjustHour(Date date, int offset) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, offset);
        Date newDate = calendar.getTime();

        return newDate;
    }

    /**
     * $时间小时偏移 <功能详细描述>
     *
     * @param time yyyyMMddHHmmss
     * @param offset 偏移量
     * @return yyyyMMddHHmmss
     * @see [类、类#方法、类#成员]
     */
    public static String adjustHour(String time, int offset) {
        return adjustHour(time, offset, YYYYMMDDHHMMSS);
    }

    /**
     * $时间小时偏移 <功能详细描述>
     *
     * @param time yyyyMMddHHmmss
     * @param offset 偏移量
     * @return yyyyMMddHHmmss
     * @see [类、类#方法、类#成员]
     */
    public static String adjustHour(String time, int offset, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String retTime = null;

        try {
            Date date = formatter.parse(time);
            Date newDate = adjustHour(date, offset);
            retTime = formatter.format(newDate);
        }
        catch (ParseException e) {
            log.error("adjustHour fail!" + ExceptionUtils.getStackTrace(e));
        }

        return retTime;
    }

    /**
     * $时间分钟偏移 <功能详细描述>
     *
     * @param date 日期
     * @param offset 偏移量
     * @return yyyyMMddHHmmss
     * @see [类、类#方法、类#成员]
     */
    public static Date adjustMinute(Date date, int offset) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, offset);
        Date newDate = calendar.getTime();
        return newDate;
    }

    /**
     * $时间分钟偏移 <功能详细描述>
     *
     * @param time yyyyMMddHHmmss
     * @param offset 偏移量
     * @return yyyyMMddHHmmss
     * @see [类、类#方法、类#成员]
     */
    public static String adjustMinute(String time, int offset) {
        return adjustMinute(time, offset, YYYYMMDDHHMMSS);
    }

    /**
     * $时间分钟偏移 <功能详细描述>
     *
     * @param time 字符串日期
     * @param offset 偏移量
     * @return yyyyMMddHHmmss
     * @see [类、类#方法、类#成员]
     */
    public static String adjustMinute(String time, int offset, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String retTime = null;

        try {
            Date date = formatter.parse(time);
            Date newDate = adjustMinute(date, offset);
            retTime = formatter.format(newDate);
        }
        catch (ParseException e) {
            log.error("adjustMinute fail!" + ExceptionUtils.getStackTrace(e));
        }

        return retTime;
    }

    /**
     * $时间秒偏移 <功能详细描述>
     *
     * @param date 字符串日期
     * @param offset 偏移量
     * @return yyyyMMddHHmmss
     * @see [类、类#方法、类#成员]
     */
    public static Date adjustSecond(Date date, int offset) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, offset);
        Date newDate = calendar.getTime();
        return newDate;
    }

    /**
     * $时间秒偏移 <功能详细描述>
     *
     * @param time yyyyMMddHHmmss
     * @param offset 偏移量
     * @return yyyyMMddHHmmss
     * @see [类、类#方法、类#成员]
     */
    public static String adjustSecond(String time, int offset) {
        return adjustSecond(time, offset, YYYYMMDDHHMMSS);
    }

    /**
     * $时间秒偏移 <功能详细描述>
     *
     * @param time yyyyMMddHHmmss
     * @param offset 偏移量
     * @return yyyyMMddHHmmss
     * @see [类、类#方法、类#成员]
     */
    public static String adjustSecond(String time, int offset, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String retTime = null;

        try {
            Date date = formatter.parse(time);
            Date newDate = adjustSecond(date, offset);
            retTime = formatter.format(newDate);
        }
        catch (ParseException e) {
            log.error("adjustSecond fail!" + ExceptionUtils.getStackTrace(e));
        }

        return retTime;
    }

    /**
     * $获取两个间隔日期间的天数 <功能详细描述>
     *
     * @param timeBefore yyyyMMdd格式
     * @param timeAfter yyyyMMdd格式
     * @return 天数
     * @see [类、类#方法、类#成员]
     */
    public static int getDayBetween(String timeBefore, String timeAfter) {
        SimpleDateFormat sd = new SimpleDateFormat("yyyyMMdd");
        long num = 0;

        try {
            long diff = sd.parse(timeAfter).getTime() - sd.parse(timeBefore).getTime();
            num = diff / (1000 * 24 * 60 * 60);
        }
        catch (ParseException e) {
            log.error("getDayBetween fail!", e);
        }

        return (int) num;
    }

    /**
     * $获取一个月最后一天
     *
     * @param time yyyyMMdd格式
     * @return 日期
     * @see [类、类#方法、类#成员]
     */
    public static String getMaxDateOfMonth(String time, String format) {
        SimpleDateFormat sd = new SimpleDateFormat(format);
        Calendar cDay = Calendar.getInstance();

        try {
            Date date = sd.parse(time);
            cDay.setTime(date);
        }
        catch (ParseException e) {
            log.error("getMaxDateOfMonth fail!", e);
        }

        return String.valueOf(cDay.getActualMaximum(Calendar.DAY_OF_MONTH));
    }

    /**
     * Date转String <br>
     * 根据format格式转换时间为字符串
     *
     * @param date 时刻
     * @param format 转换模板
     * @return 字符串日期
     * @see [类、类#方法、类#成员]
     */
    public static String dateToString(Date date, String format) {
        if (date == null) {
            date = new Date();
        }

        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String retTime = formatter.format(date);

        return retTime;
    }

    /**
     * $格式化取时间
     *
     * @param dateString yyyyMMddHHmmss时刻
     * @param format 转换模板
     * @return 字符串日期
     * @see [类、类#方法、类#成员]
     */
    public static String formater(String dateString, String format) {
        Date date = stringToDate(dateString);
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String retTime = formatter.format(date);

        return retTime;
    }

    /**
     * 格式化取时间
     * @param dateString 字符串日期
     * @param from 源格式
     * @param to 目标格式
     * @return String
     */
    public static String formater(String dateString, String from, String to) {
        Date date = stringToDate(dateString, from);
        SimpleDateFormat formatter = new SimpleDateFormat(to);
        String retTime = formatter.format(date);

        return retTime;
    }

    /**
     * $时间转换 <功能详细描述>
     *
     * @param dateKey 天
     * @param timeKey  时间
     * @param format 格式
     * @return String
     * @see [类、类#方法、类#成员]
     */
    public static String formater(int dateKey, int timeKey, String format) {
        int year = dateKey / 10000;
        int month = (dateKey / 100) % 100;
        int day = dateKey % 100;
        int hour = timeKey / 10000;
        int min = (timeKey / 100) % 100;
        int second = timeKey % 100;

        GregorianCalendar calender = new GregorianCalendar(year, month - 1, day, hour, min, second);
        return dateToString(calender.getTime(), format);
    }

    /**
     * Date转String <br>
     * 根据format格式转换时间为字符串
     *
     * @param date 时刻
     * @return 字符串日期
     * @see [类、类#方法、类#成员]
     */
    public static String dateToString(Date date) {
        return dateToString(date, YYYY_MM_DD_HH_MM_SS);
    }

    /**
     * String转Date <br>
     * 根据format格式转换时间为字符串
     *
     * @param dateString 时刻
     * @return date
     * @see [类、类#方法、类#成员]
     */
    public static Date stringToDate(String dateString) {
        return stringToDate(dateString, YYYYMMDDHHMMSS);
    }

    /**
     * String转Date
     * @param dateString 字符串日期
     * @param format 格式
     * @return date
     */
    public static Date stringToDate(String dateString, String format) {
        SimpleDateFormat sd = new SimpleDateFormat(format);
        Date date = null;

        try {
            date = sd.parse(dateString);
        }
        catch (ParseException e) {
            log.error("stringToDate fail!", e);
        }

        return date;
    }

    /**
     * String转Calendar <br>
     * 根据format格式转换时间为字符串
     *
     * @param dateString 时刻
     * @return Calendar
     * @see [类、类#方法、类#成员]
     */
    public static Calendar stringToCalendar(String dateString) {
        if ((dateString == null) || (dateString.length() < 14)) {
            throw new IllegalArgumentException(dateString);
        }

        Date date = stringToDate(dateString);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        return calendar;
    }

    /**
     * String转Calendar <br>
     * 根据format格式转换时间为字符串
     *
     * @param dateString 时间字符串
     * @param format 格式
     * @return Calendar
     * @see [类、类#方法、类#成员]
     */
    public static Calendar stringToCalendar(String dateString, String format) {
        Date date = stringToDate(dateString, format);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    /**
     * $获取当前时间 <功能详细描述>
     *
     * @return String yyyyMMddHHmmss
     * @see [类、类#方法、类#成员]
     */
    public static String getCurrentTime() {
        return dateToString(new Date(), YYYYMMDDHHMMSS);
    }

    /**
     * $获取两个时间之间的间隔（毫秒） <功能详细描述>
     *
     * @param d1 日期1
     * @param d2 日期2
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static long getTimeBetween(Date d1, Date d2) {
        return Math.abs(d2.getTime() - d1.getTime());
    }

    /**
     * $获取星期几
     *
     * @param date 输入日期
     * @return 星期几
     */
    public static int getWeekOfDate(Date date) {
        Calendar cd = Calendar.getInstance();
        cd.setTime(date);
        int wd = cd.get(Calendar.DAY_OF_WEEK);

        if (wd == 1) {
            return 7;
        }
        else {
            return wd - 1;
        }
    }

    /**
     * $早高峰区间
     */
    private static final String[] MORING_PEAK_INTERVAL = new String[] {
        "070000", "090000"
    };

    /**
     * $晚高峰区间
     */
    private static final String[] EVENING_PEAK_INTERVAL = new String[] {
        "170000", "190000"
    };

    /**
     * $平峰区间
     */
    private static final String[] FLAT_PEAK_INTERVAL = new String[] {
        "090000", "170000"
    };

    /**
     * $判断是否早高峰 <07:00~09:00>
     *
     * @param date 输入日期
     * @return boolean
     */
    public static boolean checkMoringPeak(Date date) {
        String time = StringUtils.substring(dateToString(date, YYYYMMDDHHMMSS), 8, 14);
        return StringUtils.compare(time, MORING_PEAK_INTERVAL[0]) >= 0
            && StringUtils.compare(time, MORING_PEAK_INTERVAL[1]) <= 0;
    }

    /**
     * $判断是否晚高峰 <07:00~09:00>
     *
     * @param date 输入日期
     * @return boolean
     */
    public static boolean checkEveningPeak(Date date) {
        String time = StringUtils.substring(dateToString(date, YYYYMMDDHHMMSS), 8, 14);
        return StringUtils.compare(time, EVENING_PEAK_INTERVAL[0]) >= 0
            && StringUtils.compare(time, EVENING_PEAK_INTERVAL[1]) <= 0;
    }

    /**
     * $判断是否平峰 <09:00~17:00>
     *
     * @param date 输入日期
     * @return boolean
     */
    public static boolean checkFlatPeak(Date date) {
        String time = StringUtils.substring(dateToString(date, YYYYMMDDHHMMSS), 8, 14);
        return StringUtils.compare(time, FLAT_PEAK_INTERVAL[0]) >= 0
            && StringUtils.compare(time, FLAT_PEAK_INTERVAL[1]) <= 0;
    }

    /**
     * $判断是否工作日 <功能详细描述>
     *
     * @param date 日期
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static boolean checkWorkDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int wd = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        return wd >= 1 && wd <= 5;
    }

    /**
     * $判断是否非工作日 <功能详细描述>
     *
     * @param date 日期
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static boolean checkNoWorkDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int wd = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        return wd == 6 || wd == 0;
    }

    public static Integer dateToStepIndex(String YmDHms, String tp) {
        LocalDateTime localDateTime = LocalDateTime.parse(YmDHms, DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS));
        int hour = localDateTime.getHour();
        int minute = localDateTime.getMinute();
        int stepIndex = 0;

        if (tp.endsWith("mi")) {
            int interval = Integer.parseInt(tp.split("mi")[0]);
            Double floor = Math.floor((hour * 60 + minute) / (double) interval);
            stepIndex = floor.intValue();
        }
        else if (tp.endsWith("h")) {
            stepIndex = hour;
        }
        return stepIndex;
    }

    /**
     * 计算时间序列 - 当前时刻是当天第N个时间片
     * @param period 时间片间隔
     * @param timeStr 当前时间yyyy-MM-dd HH:mm:ss
     * @return 时间序列
     */
    public static int buildColTimeIndex(long period, String timeStr) {
        long dataTp = period / 60;
        return dateToStepIndex(timeStr, dataTp + "mi");
    }

    public static String dateToUtcString(Date date){
        SimpleDateFormat sdfutc = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
        sdfutc.setTimeZone(TimeZone.getTimeZone("UTC"));
        String format = sdfutc.format(date);
        return format;
    }

    public static Date stringUtcToDate(String date) {
        SimpleDateFormat sdfutc = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        sdfutc.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date parse = null;
        try {
            parse = sdfutc.parse(date);
        } catch (ParseException e) {
            log.error("mongoDbUtcToBjDate fail!" + ExceptionUtils.getStackTrace(e));
        }

        return parse;
    }

    public static Date currentDateZeroHours(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;
        try {
            parse = simpleDateFormat.parse(simpleDateFormat.format(date));
        } catch (ParseException e) {
            log.error("currentDateZeroHours fail!" + ExceptionUtils.getStackTrace(e));
        }
        return parse;
    }

    public static Integer secondDifferenceCount(Date start,Date end,Integer frequency){
        Long count = (end.getTime()-start.getTime())/frequency;
        return count.intValue();
    }
}
