Java常用日期转换

提供一些常见的Java日期类转换代码

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @Author: 
 * @CreateTime: 
 * @Description: 时间工具类
 */
public class DateUtil {


    /**
     * 日期格式,年份,例如:2004,2008
     */
    public static final String DATE_FORMAT_YYYY = "yyyy";


    /**
     * 日期格式,年份和月份,例如:200707,200808
     */
    public static final String DATE_FORMAT_YYYYMM = "yyyyMM";

    /**
     * 日期格式,年份和月份,例如:200707,2008-08
     */
    public static final String DATE_FORMAT_YYYY_MM = "yyyy-MM";


    /**
     * 日期格式,年月日,例如:050630,080808
     */
    public static final String DATE_FORMAT_YYMMDD = "yyMMdd";

    /**
     * 日期格式,年月日,用横杠分开,例如:06-12-25,08-08-08
     */
    public static final String DATE_FORMAT_YY_MM_DD = "yy-MM-dd";

    /**
     * 日期格式,年月日,例如:20050630,20080808
     */
    public static final String DATE_FORMAT_YYYYMMDD = "yyyyMMdd";

    /**
     * 日期格式,年月日,用横杠分开,例如:2006-12-25,2008-08-08
     */
    public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";

    /**
     * 日期格式,年月日,例如:2016.10.05
     */
    public static final String DATE_FORMAT_POINTYYYYMMDD = "yyyy.MM.dd";

    /**
     * 日期格式,年月日,例如:2016年10月05日
     */
    public static final String DATE_TIME_FORMAT_YYYY年MM月DD日 = "yyyy年MM月dd日";

    /**
     * 日期格式,年月日时分,例如:200506301210,200808081210
     */
    public static final String DATE_FORMAT_YYYYMMDDHHmm = "yyyyMMddHHmm";

    /**
     * 日期格式,年月日时分,例如:20001230 12:00,20080808 20:08
     */
    public static final String DATE_TIME_FORMAT_YYYYMMDD_HH_MI = "yyyyMMdd HH:mm";

    /**
     * 日期格式,年月日时分,例如:2000-12-30 12:00,2008-08-08 20:08
     */
    public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI = "yyyy-MM-dd HH:mm";

    /**
     * 日期格式,年月日时分秒,例如:20001230120000,20080808200808
     */
    public static final String DATE_TIME_FORMAT_YYYYMMDDHHMISS = "yyyyMMddHHmmss";

    /**
     * 日期格式,年月日时分秒,年月日用横杠分开,时分秒用冒号分开
     * 例如:2005-05-10 23:20:00,2008-08-08 20:08:08
     */
    public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS = "yyyy-MM-dd HH:mm:ss";


    public static final String DATE_TIME_FORMAT_HH_MI_SS = "HH:mm:ss";

    /**
     * 日期格式,年月日时分秒毫秒,例如:20001230120000123,20080808200808456
     */
    public static final String DATE_TIME_FORMAT_YYYYMMDDHHMISSSSS = "yyyyMMddHHmmssSSS";


    // ==特殊格式==
    /**
     * 日期格式,月日时分,例如:10-05 12:00
     */
    public static final String DATE_FORMAT_MMDDHHMI = "MM-dd HH:mm";

    /**
     * ISO8601标准时间格式
     */
    public static final String ISO8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";

    /* ************工具方法***************   */

    /**
     * 获取某日期的年份
     *
     * @param date
     * @return
     */
    public static Integer getYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.YEAR);
    }

    /**
     * 获取某日期的月份
     *
     * @param date
     * @return
     */
    public static Integer getMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.MONTH) + 1;
    }

    /**
     * 获取某日期的日数
     *
     * @param date
     * @return
     */
    public static Integer getDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int day = cal.get(Calendar.DATE);//获取日
        return day;
    }

    /**
     * 获取某日期的小时
     *
     * @param date
     * @return
     */
    public static Integer getHour(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int hour = cal.get(Calendar.HOUR_OF_DAY);//获取小时
        return hour;
    }

    /**
     * 获取某日期的分钟
     *
     * @param date
     * @return
     */
    public static Integer getMinute(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int minute = cal.get(Calendar.MINUTE);//获取小时
        return minute;
    }

    /**
     * 获取某日期的秒
     *
     * @param date
     * @return
     */
    public static Integer getSecond(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int second = cal.get(Calendar.SECOND);//获取小时
        return second;
    }

    /**
     * 格式化Date时间
     *
     * @param time       Date类型时间
     * @param timeFromat String类型格式
     * @return 格式化后的字符串
     */
    public static String parseDateToStr(Date time, String timeFromat) {
        DateFormat dateFormat = new SimpleDateFormat(timeFromat);
        return dateFormat.format(time);
    }

    /**
     * 格式化Timestamp时间
     *
     * @param timestamp  Timestamp类型时间
     * @param timeFromat
     * @return 格式化后的字符串
     */
    public static String parseTimestampToStr(Timestamp timestamp, String timeFromat) {
        SimpleDateFormat df = new SimpleDateFormat(timeFromat);
        return df.format(timestamp);
    }

    /**
     * 格式化Date时间
     *
     * @param time         Date类型时间
     * @param timeFromat   String类型格式
     * @param defaultValue 默认值为当前时间Date
     * @return 格式化后的字符串
     */
    public static String parseDateToStr(Date time, String timeFromat, final Date defaultValue) {
        try {
            DateFormat dateFormat = new SimpleDateFormat(timeFromat);
            return dateFormat.format(time);
        } catch (Exception e) {
            if (defaultValue != null)
                return parseDateToStr(defaultValue, timeFromat);
            else
                return parseDateToStr(new Date(), timeFromat);
        }
    }

    /**
     * 格式化Date时间
     *
     * @param time         Date类型时间
     * @param timeFromat   String类型格式
     * @param defaultValue 默认时间值String类型
     * @return 格式化后的字符串
     */
    public static String parseDateToStr(Date time, String timeFromat, final String defaultValue) {
        try {
            DateFormat dateFormat = new SimpleDateFormat(timeFromat);
            return dateFormat.format(time);
        } catch (Exception e) {
            return defaultValue;
        }
    }

    /**
     * 格式化String时间
     *
     * @param time       String类型时间
     * @param timeFromat String类型格式
     * @return 格式化后的Date日期
     */
    public static Date parseStrToDate(String time, String timeFromat) {
        if (time == null || time.equals("")) {
            return null;
        }

        Date date = null;
        try {
            DateFormat dateFormat = new SimpleDateFormat(timeFromat);
            date = dateFormat.parse(time);
        } catch (Exception e) {

        }
        return date;
    }

    /**
     * 格式化String时间
     *
     * @param strTime      String类型时间
     * @param timeFromat   String类型格式
     * @param defaultValue 异常时返回的默认值
     * @return
     */
    public static Date parseStrToDate(String strTime, String timeFromat,
                                      Date defaultValue) {
        try {
            DateFormat dateFormat = new SimpleDateFormat(timeFromat);
            return dateFormat.parse(strTime);
        } catch (Exception e) {
            return defaultValue;
        }
    }

    /**
     * 当strTime为2008-9时返回为2008-9-1 00:00格式日期时间,无法转换返回null.
     *
     * @param strTime
     * @return
     */
    public static Date strToDate(String strTime) {
        if (strTime == null || strTime.trim().length() <= 0)
            return null;

        Date date = null;
        List<String> list = new ArrayList<String>(0);

        list.add(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        list.add(DATE_TIME_FORMAT_YYYYMMDDHHMISSSSS);
        list.add(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI);
        list.add(DATE_TIME_FORMAT_YYYYMMDD_HH_MI);
        list.add(DATE_TIME_FORMAT_YYYYMMDDHHMISS);
        list.add(DATE_FORMAT_YYYY_MM_DD);
        //list.add(DATE_FORMAT_YY_MM_DD);
        list.add(DATE_FORMAT_YYYYMMDD);
        list.add(DATE_FORMAT_YYYY_MM);
        list.add(DATE_FORMAT_YYYYMM);
        list.add(DATE_FORMAT_YYYY);


        for (Iterator iter = list.iterator(); iter.hasNext(); ) {
            String format = (String) iter.next();
            if (strTime.indexOf("-") > 0 && format.indexOf("-") < 0)
                continue;
            if (strTime.indexOf("-") < 0 && format.indexOf("-") > 0)
                continue;
            if (strTime.length() > format.length())
                continue;
            date = parseStrToDate(strTime, format);
            if (date != null)
                break;
        }

        return date;
    }

    /**
     * 解析两个日期之间的所有月份
     *
     * @param beginDateStr 开始日期,至少精确到yyyy-MM
     * @param endDateStr   结束日期,至少精确到yyyy-MM
     * @return yyyy-MM日期集合
     */
    public static List<String> getMonthListOfDate(String beginDateStr, String endDateStr) {
        // 指定要解析的时间格式
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM");
        // 返回的月份列表
        String sRet = "";

        // 定义一些变量
        Date beginDate = null;
        Date endDate = null;

        GregorianCalendar beginGC = null;
        GregorianCalendar endGC = null;
        List<String> list = new ArrayList<String>();

        try {
            // 将字符串parse成日期
            beginDate = f.parse(beginDateStr);
            endDate = f.parse(endDateStr);

            // 设置日历
            beginGC = new GregorianCalendar();
            beginGC.setTime(beginDate);

            endGC = new GregorianCalendar();
            endGC.setTime(endDate);

            // 直到两个时间相同
            while (beginGC.getTime().compareTo(endGC.getTime()) <= 0) {
                sRet = beginGC.get(Calendar.YEAR) + "-"
                        + (beginGC.get(Calendar.MONTH) + 1);
                list.add(sRet);
                // 以月为单位,增加时间
                beginGC.add(Calendar.MONTH, 1);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 解析两个日期段之间的所有日期
     *
     * @param beginDateStr 开始日期  ,至少精确到yyyy-MM-dd
     * @param endDateStr   结束日期  ,至少精确到yyyy-MM-dd
     * @return yyyy-MM-dd日期集合
     */
    public static List<String> getDayListOfDate(String beginDateStr, String endDateStr) {
        // 指定要解析的时间格式
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");

        // 定义一些变量
        Date beginDate = null;
        Date endDate = null;

        Calendar beginGC = null;
        Calendar endGC = null;
        List<String> list = new ArrayList<String>();

        try {
            // 将字符串parse成日期
            beginDate = f.parse(beginDateStr);
            endDate = f.parse(endDateStr);

            // 设置日历
            beginGC = Calendar.getInstance();
            beginGC.setTime(beginDate);

            endGC = Calendar.getInstance();
            endGC.setTime(endDate);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

            // 直到两个时间相同
            while (beginGC.getTime().compareTo(endGC.getTime()) <= 0) {

                list.add(sdf.format(beginGC.getTime()));
                // 以日为单位,增加时间
                beginGC.add(Calendar.DAY_OF_MONTH, 1);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static List<String> getDayListOfDate(String beginDateStr, String endDateStr,String format) {
        // 指定要解析的时间格式
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");

        // 定义一些变量
        Date beginDate = null;
        Date endDate = null;

        Calendar beginGC = null;
        Calendar endGC = null;
        List<String> list = new ArrayList<String>();

        try {
            // 将字符串parse成日期
            beginDate = f.parse(beginDateStr);
            endDate = f.parse(endDateStr);

            // 设置日历
            beginGC = Calendar.getInstance();
            beginGC.setTime(beginDate);

            endGC = Calendar.getInstance();
            endGC.setTime(endDate);
            SimpleDateFormat sdf = new SimpleDateFormat(format);

            // 直到两个时间相同
            while (beginGC.getTime().compareTo(endGC.getTime()) <= 0) {

                list.add(sdf.format(beginGC.getTime()));
                // 以日为单位,增加时间
                beginGC.add(Calendar.DAY_OF_MONTH, 1);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取当下年份指定前后数量的年份集合
     *
     * @param before 当下年份前年数
     * @param behind 当下年份后年数
     * @return 集合
     */
    public static List<Integer> getYearListOfYears(int before, int behind) {
        if (before < 0 || behind < 0) {
            return null;
        }
        List<Integer> list = new ArrayList<Integer>();
        Calendar c = null;
        c = Calendar.getInstance();
        c.setTime(new Date());
        int currYear = Calendar.getInstance().get(Calendar.YEAR);

        int startYear = currYear - before;
        int endYear = currYear + behind;
        for (int i = startYear; i < endYear; i++) {
            list.add(Integer.valueOf(i));
        }
        return list;
    }

    /**
     * 获取当前日期是一年中第几周
     *
     * @param date
     * @return
     */
    public static Integer getWeekthOfYear(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setMinimalDaysInFirstWeek(7);
        c.setTime(date);

        return c.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 获取某一年各星期的始终时间
     * 实例:getWeekList(2016),第52周(从2016-12-26至2017-01-01)
     *
     * @param year
     * @return
     */
    public static HashMap<Integer, String> getWeekTimeOfYear(int year) {
        HashMap<Integer, String> map = new LinkedHashMap<Integer, String>();
        Calendar c = new GregorianCalendar();
        c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
        int count = getWeekthOfYear(c.getTime());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dayOfWeekStart = "";
        String dayOfWeekEnd = "";
        for (int i = 1; i <= count; i++) {
            dayOfWeekStart = sdf.format(getFirstDayOfWeek(year, i));
            dayOfWeekEnd = sdf.format(getLastDayOfWeek(year, i));
            map.put(Integer.valueOf(i), "第" + i + "周(从" + dayOfWeekStart + "至" + dayOfWeekEnd + ")");
        }
        return map;

    }

    /**
     * 获取某一年的总周数
     *
     * @param year
     * @return
     */
    public static Integer getWeekCountOfYear(int year) {
        Calendar c = new GregorianCalendar();
        c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
        int count = getWeekthOfYear(c.getTime());
        return count;
    }

    /**
     * 获取指定日期所在周的第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
        return c.getTime();
    }

    /**
     * 获取指定日期所在周的最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
        return c.getTime();
    }

    /**
     * 获取某年某周的第一天
     *
     * @param year 目标年份
     * @param week 目标周数
     * @return
     */
    public static Date getFirstDayOfWeek(int year, int week) {
        Calendar c = new GregorianCalendar();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, Calendar.JANUARY);
        c.set(Calendar.DATE, 1);

        Calendar cal = (GregorianCalendar) c.clone();
        cal.add(Calendar.DATE, week * 7);

        return getFirstDayOfWeek(cal.getTime());
    }

    /**
     * 获取某年某周的最后一天
     *
     * @param year 目标年份
     * @param week 目标周数
     * @return
     */
    public static Date getLastDayOfWeek(int year, int week) {
        Calendar c = new GregorianCalendar();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, Calendar.JANUARY);
        c.set(Calendar.DATE, 1);

        Calendar cal = (GregorianCalendar) c.clone();
        cal.add(Calendar.DATE, week * 7);

        return getLastDayOfWeek(cal.getTime());
    }

    /**
     * 获取某年某月的第一天
     *
     * @param year  目标年份
     * @param month 目标月份
     * @return
     */
    public static Date getFirstDayOfMonth(int year, int month) {
        month = month - 1;
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);

        int day = c.getActualMinimum(c.DAY_OF_MONTH);

        c.set(Calendar.DAY_OF_MONTH, day);
        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();
    }

    /**
     * 获取某年某月的最后一天
     *
     * @param year  目标年份
     * @param month 目标月份
     * @return
     */
    public static Date getLastDayOfMonth(int year, int month) {
        month = month - 1;
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        int day = c.getActualMaximum(c.DAY_OF_MONTH);
        c.set(Calendar.DAY_OF_MONTH, day);
        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();
    }

    /**
     * 获取某个日期为星期几
     *
     * @param date
     * @return String "星期*"
     */
    public static String getDayWeekOfDate1(Date date) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;

        return weekDays[w];
    }

    /**
     * 获得指定日期的星期几数
     *
     * @param date
     * @return int
     */
    public static Integer getDayWeekOfDate2(Date date) {
        Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTime(date);
        int weekDay = aCalendar.get(Calendar.DAY_OF_WEEK);
        return weekDay;
    }

    /**
     * 验证字符串是否为日期
     * 验证格式:YYYYMMDD、YYYY_MM_DD、YYYYMMDDHHMISS、YYYYMMDD_HH_MI、YYYY_MM_DD_HH_MI、YYYYMMDDHHMISSSSS、YYYY_MM_DD_HH_MI_SS
     *
     * @param strTime
     * @return null时返回false;true为日期,false不为日期
     */
    public static boolean validateIsDate(String strTime) {
        if (strTime == null || strTime.trim().length() <= 0)
            return false;

        Date date = null;
        List<String> list = new ArrayList<String>(0);

        list.add(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        list.add(DATE_TIME_FORMAT_YYYYMMDDHHMISSSSS);
        list.add(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI);
        list.add(DATE_TIME_FORMAT_YYYYMMDD_HH_MI);
        list.add(DATE_TIME_FORMAT_YYYYMMDDHHMISS);
        list.add(DATE_FORMAT_YYYY_MM_DD);
        //list.add(DATE_FORMAT_YY_MM_DD);
        list.add(DATE_FORMAT_YYYYMMDD);
        //list.add(DATE_FORMAT_YYYY_MM);
        //list.add(DATE_FORMAT_YYYYMM);
        //list.add(DATE_FORMAT_YYYY);

        for (Iterator iter = list.iterator(); iter.hasNext(); ) {
            String format = (String) iter.next();
            if (strTime.indexOf("-") > 0 && format.indexOf("-") < 0)
                continue;
            if (strTime.indexOf("-") < 0 && format.indexOf("-") > 0)
                continue;
            if (strTime.length() > format.length())
                continue;
            date = parseStrToDate(strTime.trim(), format);
            if (date != null)
                break;
        }

        if (date != null) {
            System.out.println("生成的日期:" + DateUtil.parseDateToStr(date, DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS, "--null--"));
            return true;
        }
        return false;
    }

    /**
     * 将指定日期的时分秒格式为零
     *
     * @param date
     * @return
     */
    public static Date formatHhMmSsOfDate(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();
    }

    /**
     * 获得指定时间加减参数后的日期(不计算则输入0)
     *
     * @param date        指定日期
     * @param year        年数,可正可负
     * @param month       月数,可正可负
     * @param day         天数,可正可负
     * @param hour        小时数,可正可负
     * @param minute      分钟数,可正可负
     * @param second      秒数,可正可负
     * @param millisecond 毫秒数,可正可负
     * @return 计算后的日期
     */
    public static Date addDate(Date date, int year, int month, int day, int hour, int minute, int second, int millisecond) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.YEAR, year);//加减年数
        c.add(Calendar.MONTH, month);//加减月数
        c.add(Calendar.DATE, day);//加减天数
        c.add(Calendar.HOUR, hour);//加减小时数
        c.add(Calendar.MINUTE, minute);//加减分钟数
        c.add(Calendar.SECOND, second);//加减秒
        c.add(Calendar.MILLISECOND, millisecond);//加减毫秒数

        return c.getTime();
    }

    /**
     * 获得两个日期的时间戳之差
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static Long getDistanceTimestamp(Date startDate, Date endDate) {
        long daysBetween = (endDate.getTime() - startDate.getTime() + 1000000) / (3600 * 24 * 1000);
        return daysBetween;
    }

    /**
     * 判断二个时间是否为同年同月
     *
     * @param date1
     * @param date2
     * @return
     */
    public static Boolean compareIsSameMonth(Date date1, Date date2) {
        boolean flag = false;
        int year1 = getYear(date1);
        int year2 = getYear(date2);
        if (year1 == year2) {
            int month1 = getMonth(date1);
            int month2 = getMonth(date2);
            if (month1 == month2) flag = true;
        }
        return flag;
    }

    /**
     * 获得两个时间相差距离多少天多少小时多少分多少秒
     *
     * @param str1 时间参数 1 格式:1990-01-01 12:00:00
     * @param str2 时间参数 2 格式:2009-01-01 12:00:00
     * @return long[] 返回值为:{天, 时, 分, 秒}
     */
    public static long[] getDistanceTime(Date str1, Date str2) {
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        try {

            long time1 = str1.getTime();
            long time2 = str2.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            day = diff / (24 * 60 * 60 * 1000);
            hour = diff / (60 * 60 * 1000) - day * 24;
            min = diff / (60 * 1000) - day * 24 * 60 - hour * 60;
            sec = diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60;
        } catch (Exception e) {
            e.printStackTrace();
        }
        long[] times = {day, hour, min, sec};
        return times;
    }

    /**
     * 两个时间相差距离多少天多少小时多少分多少秒
     *
     * @param str1 时间参数 1 格式:1990-01-01 12:00:00
     * @param str2 时间参数 2 格式:2009-01-01 12:00:00
     * @return String 返回值为:{天, 时, 分, 秒}
     */
    public static long[] getDistanceTime(String str1, String str2) {
        DateFormat df = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        Date one;
        Date two;
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            day = diff / (24 * 60 * 60 * 1000);
            hour = diff / (60 * 60 * 1000) - day * 24;
            min = diff / (60 * 1000) - day * 24 * 60 - hour * 60;
            sec = diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long[] times = {day, hour, min, sec};
        return times;
    }

    /**
     * 两个时间之间相差距离多少天
     *
     * @param str1 时间参数 1:
     * @param str2 时间参数 2:
     * @return 相差天数
     */
    public static Long getDistanceDays(String str1, String str2) throws Exception {
        DateFormat df = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        Date one;
        Date two;
        long days = 0;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            days = diff / (1000 * 60 * 60 * 24);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    /**
     * 获取指定时间的那天 00:00:00.000 的时间
     *
     * @param date
     * @return
     */
    public static Date getDayBeginTime(final Date date) {
        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();
    }

    /**
     * 获取指定时间的那天 23:59:59.999 的时间
     *
     * @param date
     * @return
     */
    public static Date getDayEndTime(final Date date) {
        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();
    }


    /**
     * 获取ISO8601时间格式的时间字符串
     *
     * @param dateTime
     * @return
     */
    public static String formatISO8601(Date dateTime) {
        DateFormat sdf = new SimpleDateFormat(ISO8601_DATE_FORMAT);
        return sdf.format(dateTime);
    }

    /**
     * 获取ISO8601时间格式的时间
     *
     * @param dateTime
     * @return
     */
    public static Date fromISO8601(String dateTime) {
        try {
            return new SimpleDateFormat(ISO8601_DATE_FORMAT).parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取yyyy-MM-dd HH:mm:ss格式的时间字符串
     *
     * @param dateTime
     * @return
     */
    public static String formatDateTime(Date dateTime) {
        DateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        return sdf.format(dateTime);
    }

    /**
     * 获取yyyy-MM-dd HH:mm:ss时间格式的时间
     *
     * @param dateTime
     * @return
     */
    public static Date fromDateTime(String dateTime) {
        try {
            return new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS).parse(dateTime);
        } catch (ParseException e) {
            logger.error("时间格式转化异常!", e.toString());
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 将yyyyMMddHHmmss形式的时间转化为yyyy-MM-dd'T'HH:mm:ss.SSSXXX格式的时间
     *
     * @param dateTime
     * @return
     */
    public static String formatISO8601FromYYMMDDHHmmss(String dateTime) {
        try {
            DateFormat newSdf = new SimpleDateFormat(ISO8601_DATE_FORMAT);
            DateFormat oldSdf = new SimpleDateFormat(DATE_TIME_FORMAT_YYYYMMDDHHMISS);
            Date oldDate = oldSdf.parse(dateTime);
            return newSdf.format(oldDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将yyyy-MM-dd HH:mm:ss形式的时间转化为yyyy-MM-dd'T'HH:mm:ss.SSSXXX格式的时间
     *
     * @param dateTime
     * @return
     */
    public static String formatISO8601FromStandard(String dateTime) {
        try {
            DateFormat newSdf = new SimpleDateFormat(ISO8601_DATE_FORMAT);
            DateFormat oldSdf = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
            Date oldDate = oldSdf.parse(dateTime);
            return newSdf.format(oldDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将yyyy-MM-dd'T'HH:mm:ss.SSSXXX格式的时间转化为yyyy-MM-dd HH:mm:ss形式的时间
     *
     * @param dateTime
     * @return
     */
    public static String formatStandardFromISO8601(String dateTime) {
        try {
            DateFormat newSdf = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
            DateFormat oldSdf = new SimpleDateFormat(ISO8601_DATE_FORMAT);
            Date oldDate = oldSdf.parse(dateTime);
            return newSdf.format(oldDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String formatYMDDate(Date dateTime) {
        DateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
        return sdf.format(dateTime);
    }

    public static String formatHHHISS(Date dateTime) {
        DateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT_HH_MI_SS);
        return sdf.format(dateTime);
    }

    public static String formatYMDHMSDate(Date dateTime) {
        DateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        return sdf.format(dateTime);
    }

    //将时间格式转换成20191212格式
    public static String formatYYYYMMDDDate(Date dateTime) {
        DateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(dateTime);
    }

    public static String getBefore1MinuteTime() {
        Date curentDate = new Date();
        Date before1MinuteDate = DateUtil.addDate(curentDate, 0, 0, 0, 0, -1, 0, 0);

        String endDateTime = formatYMDHMSDate(before1MinuteDate);

        DateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);

        Date endDateTimeISOISO8601 = null;
        try {
            endDateTimeISOISO8601 = sdf.parse(endDateTime);
        } catch (ParseException e) {
            logger.error("时间转化发生异常!");
        }
        String oneMinuteAgo = formatISO8601(endDateTimeISOISO8601);
        return oneMinuteAgo;
    }

    public static String getBefore60seTime() {
        Date curentDate = new Date();
        Date before1MinuteDate = DateUtil.addDate(curentDate, 0, 0, 0, 0, 0, -60, 0);

        String endDateTime = formatYMDHMSDate(before1MinuteDate);

        DateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);

        Date endDateTimeISOISO8601 = null;
        try {
            endDateTimeISOISO8601 = sdf.parse(endDateTime);
        } catch (ParseException e) {
            logger.error("时间转化发生异常!");
        }
        String oneMinuteAgo = formatISO8601(endDateTimeISOISO8601);
        return oneMinuteAgo;
    }

    public static String getBefore7day() {
        Date curentDate = new Date();
        Date before1MinuteDate = DateUtil.addDate(curentDate, 0, 0, -7, 0, 0, 0, 0);

        String endDateTime = formatYMDHMSDate(before1MinuteDate);

        DateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);

        Date endDateTimeISOISO8601 = null;
        try {
            endDateTimeISOISO8601 = sdf.parse(endDateTime);
        } catch (ParseException e) {
            logger.error("时间转化发生异常!");
        }
        String oneMinuteAgo = formatISO8601(endDateTimeISOISO8601);
        return oneMinuteAgo;
    }

    public static List<String> getCurrentAndBefore7DaysBeginAndEndDateTime() {
        List<String> list = new ArrayList<>();
        Date curentDate = new Date();
        Date before7DaysDate = DateUtil.addDate(curentDate, 0, 0, -6, 0, 0, 0, 0);

        String endDateTime = formatYMDDate(curentDate) + " 00:00:00";
        String beginDateTime = formatYMDDate(before7DaysDate) + " 23:59:59";


        DateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);

        Date beginDateTimeISO8601 = null;
        Date endDateTimeISOISO8601 = null;
        try {
            beginDateTimeISO8601 = sdf.parse(beginDateTime);
            endDateTimeISOISO8601 = sdf.parse(endDateTime);
        } catch (ParseException e) {
            logger.error("时间转化发生异常!");
        }
        list.add(formatISO8601(beginDateTimeISO8601));
        list.add(formatISO8601(endDateTimeISOISO8601));
        return list;
    }

    public static List<String> getCurrentAndBefore30DaysBeginAndEndDateTime() {
        List<String> list = new ArrayList<>();
        Date curentDate = new Date();
        Date before30DaysDate = DateUtil.addDate(curentDate, 0, 0, -29, 0, 0, 0, 0);

        String endDateTime = formatYMDDate(curentDate) + " 00:00:00";
        String beginDateTime = formatYMDDate(before30DaysDate) + " 23:59:59";


        DateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);

        Date beginDateTimeISO8601 = null;
        Date endDateTimeISOISO8601 = null;
        try {
            beginDateTimeISO8601 = sdf.parse(beginDateTime);
            endDateTimeISOISO8601 = sdf.parse(endDateTime);
        } catch (ParseException e) {
            logger.error("时间转化发生异常!");
        }
        list.add(formatISO8601(beginDateTimeISO8601));
        list.add(formatISO8601(endDateTimeISOISO8601));
        return list;
    }

    /**
     * 将2019-09-08T17:25:08.000+08:00格式的时间转化为DATE_FORMAT_YYYYMMDDHHmm = "yyyyMMddHHmm"格式的时间
     *
     * @param dateTime
     * @return
     */
    public static String getFormatYYMMDDhhmmss(String dateTime) {
        try {
            DateFormat oldSdf = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
            DateFormat newSdf = new SimpleDateFormat(DATE_TIME_FORMAT_YYYYMMDDHHMISS);
            String dateTimeStr = dateTime.substring(0, 10) + " " + dateTime.substring(11, 19);
            Date newDateTime = oldSdf.parse(dateTimeStr);
            return newSdf.format(newDateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取当前日期时间
     *
     * @return
     */
    public static String getCurrentDateTime() {
        DateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        Date date = new Date();
        String dateTime = sdf.format(date);
        return dateTime;
    }

    /**
     * 获取当天开始日期时间
     *
     * @return
     */
    public static String getCurrentBeginDateTime() {
        DateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
        Date date = new Date();
        String currentDate = sdf.format(date);
        String todatBeginDateTime = currentDate + " 00:00:00";
        return todatBeginDateTime;
    }

    /**
     * 获取当天向前推n天的日期时间
     *
     * @param n
     * @return
     */
    public static String getCurrentBeforeNDateTime(int n) {
        DateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
        Date curentDate = new Date();
        Date beforeNDaysDateTime = DateUtil.addDate(curentDate, 0, 0, 0 - n, 0, 0, 0, 0);
        String currentDate = sdf.format(beforeNDaysDateTime);
        String todatBeforeNDaysDateTime = currentDate + " 00:00:00";
        return todatBeforeNDaysDateTime;
    }


    /**
     * 获取指定时间的那天 00:00:00 的时间,返回格式yyyy-MM-dd HH:mm:ss
     *
     * @param date
     * @return
     */
    public static String getDayBeginTimeS(final Date date) {
        SimpleDateFormat df = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        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 df.format(c.getTime());
    }

    /**
     * 获取指定时间的那天 23:59:59的时间,返回格式yyyy-MM-dd HH:mm:ss
     *
     * @param date
     * @return
     */
    public static String getDayEndTimeS(final Date date) {
        SimpleDateFormat df = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        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 df.format(c.getTime());
    }


    /**
     * 将20191203143836的String时间转化为以毫秒为单位的时间戳(String)
     *
     * @param dateTime
     * @return
     */
    public static String fromTimeToTimestampMs(String dateTime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        try {
            Date date = simpleDateFormat.parse(dateTime);
            long ts = date.getTime();
            String res = String.valueOf(ts);
            return res;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将以毫秒为单位的时间戳(String)转化为2019-12-03 14:38:36的String时间
     *
     * @param dateTime
     * @return
     */
    public static String fromTimestampMsToTime(String dateTime) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        long lt = Long.valueOf(dateTime);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 将String转换为Date
     *
     * @param dateTime
     * @return
     */
    public static Date stringToDate(String dateTime) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = fmt.parse(dateTime);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将String转换为String
     *
     * @param dateTime
     * @param fromFormat
     * @param Toformat
     * @return
     */
    public static String stringToString(String dateTime, String fromFormat, String Toformat) {
        SimpleDateFormat fmt = new SimpleDateFormat(fromFormat);
        try {
            Date date = fmt.parse(dateTime);
            return parseDateToStr(date, Toformat);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 时间戳转换成日期格式字符串
     * @param seconds 精确到秒的字符串
     * @return
     */
    public static String timeStamp2Date(String seconds,String format) {
        if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
            return "";
        }
        if(format == null || format.isEmpty()){
            format = DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds)));
    }
    /**
     * 日期格式字符串转换成时间戳
     * @param date_str 字符串日期
     * @param format 如:yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String date2TimeStamp(String date_str,String format){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return String.valueOf(sdf.parse(date_str).getTime()/1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 取得当前时间戳(精确到秒)
     * @return
     */
    public static String timeStamp(){
        long time = System.currentTimeMillis();
        String t = String.valueOf(time/1000);
        return t;
    }

    /**
     * 获取过去第几天的最早日期
     *
     * @param past
     * @return
     */
    public static String getPastDateEarliestTime(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'00:00:00.000XXX");// ("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }

    /**
     * 获取过去第几天的最早日期(标准格式)
     *
     * @param past
     * @return
     */
    public static String getPastEarliestTime(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");// ("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }

    /**
     * 获取过去第几天的最晚日期
     *
     * @param past
     * @return
     */
    public static String getPastDateLastestTime(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'23:59:59.999XXX");// ("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }

    /**
     * 获取过去第几天的最晚日期(标准格式)
     *
     * @param past
     * @return
     */
    public static String getPastLastestTime(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 23:59:59");// ("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }

    /**
     * 获取上月环比日期
     * @param date 本月日期
     * @param formatPattern  日期的匹配格式
     * @return  获取上月环比日期
     */
    public static String  getLastMouthDate( String date,String formatPattern){
        Date lastMouth=new Date();
        try {
            SimpleDateFormat format = new SimpleDateFormat(formatPattern);
            Date currentDate = format.parse(date);
            Calendar c = Calendar.getInstance();
            c.setTime(currentDate);
            c.add(Calendar.MONTH, -1);
            lastMouth = c.getTime();

        } catch (ParseException e) {
            logger.error("获取环比日期异常",e);
        }
        SimpleDateFormat sdf = new SimpleDateFormat(formatPattern);
        return sdf.format(lastMouth);
    }

    /**
     * 获取去年同比日期
     * @param date 今年对应的日期
     * @param formatPattern  日期的匹配格式
     * @return 获取去年同比日期
     */
    public static String  getLastYearDate( String date,String formatPattern){
        Date lastYear=new Date();
        try {
            SimpleDateFormat format = new SimpleDateFormat(formatPattern);
            Date currentDate = format.parse(date);
            Calendar c = Calendar.getInstance();
            c.setTime(currentDate);
            c.add(Calendar.YEAR, -1);
            lastYear = c.getTime();
        } catch (ParseException e) {
            logger.error("获取同比日期异常",e);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        return sdf.format(lastYear);
    }

    /**
     *获取输入时间的前一天的时间
     * @param strData
     * @return: java.lang.String
     * @author wujiyuan
     * @date: 2021/10/15 16:26
     */
    public static String getPreDateByDate(String strData) {
        String preDate = "";
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(strData);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        c.setTime(date);
        int day1 = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day1 - 1);
        SimpleDateFormat sd =new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        preDate = sd.format(c.getTime());
        return preDate;
    }

    /**
     * 获取输入时间的后一天的时间
     * @param strData
     * @return: java.lang.String
     * @author wujiyuan
     * @date: 2021/10/15 16:30
     */
    public static String getNextDateByDate(String strData) {
        String preDate = "";
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(strData);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        c.setTime(date);
        int day1 = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day1 + 1);
        SimpleDateFormat sd =new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        preDate = sd.format(c.getTime());
        return preDate;
    }

    /**
     * 对时间进行加减
     * @param startDate
     * @param year
     * @param month
     * @param day
     * @param hour
     * @param minute
     * @param second
     * @param millisecond
     * @param format
     * @return: java.lang.String
     */
    public static String getNewTime(Date startDate,int year,int month,int day,int hour,int minute,int second,int millisecond,String format) {

        Date endDate = DateUtil.addDate(startDate,year,month,day,hour,minute,second,millisecond);
        Date currentDate = new Date();
        int result = currentDate.compareTo(endDate);
        if(result<0){
            endDate = currentDate;
        }
        String newTime = DateUtil.parseDateToStr(endDate,format);

        return newTime;

    }

    //把utc格式日期转换为常见的格式
    public static String utc2StrTime(String timeStr) {
        try {
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf1.parse(timeStr);
            return sdf2.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[utc2StrTime error]", e);
        }
        return timeStr;
    }

    //把utc格式日期转换为常见的格式
    public static Date utc2DateTime(String timeStr) {
        try {
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
            //SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf1.parse(timeStr);
            return date;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[utc2DateTime error]", e);
        }
        return null;
    }

}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值