java DateUtils

    public static String YYYY = "yyyy";

    public static String MM_DD = "MM-dd";

    public static String HH_MM_SS = "HH:mm:ss";

    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 + "分钟";
    }

    /**
     * 返回指定日期前后N天的日期
     *
     * @param date 日期
     * @param nDay 前后N天 正为后 负为前
     * @return
     */
    public static String getLastOrNextNDay(LocalDate date, int nDay) {
        LocalDate dateTime = date.plusDays(nDay);
        return dateTime.toString();
    }

    /**
     * 返回指定日期前N个月的月份集合
     * 
     * @param date   日期
     * @param nMonth 前N个月
     * @return
     */
    public static List getLastNMonths(LocalDate date, int nMonth) {
        List months = new ArrayList<>(nMonth);
        for (int i = nMonth; i > 0; i--) {
            LocalDate localDate = date.minusMonths(i);
            String ss = localDate.toString().substring(0, 7);
            months.add(ss);
        }
        return months;
    }

    /**
     * 取得与原日期相差一定天数的日期,返回Date型日期
     * 
     * @param date       原日期
     * @param intBetween 相差的天数
     * @return date加上intBetween天后的日期
     */
    public static Date getDateBetween(Date date, int intBetween) {
        Calendar calo = Calendar.getInstance();
        calo.setTime(date);
        calo.add(Calendar.DATE, intBetween);
        return calo.getTime();
    }

    /**
     * 按指定格式取得与原日期相差一定天数的日期,返回String型日期
     * 
     * @param date       原日期
     * @param intBetween 相差的日期 正数为后 负数为前
     * @param strFromat  返回日期的格式
     * @return date加上intBetween天后的日期
     */
    public static String getDateBetween_String(Date date, int intBetween, String strFromat) {
        Date dateOld = getDateBetween(date, intBetween);
        return getFomartDate(dateOld, strFromat);
    }

    /**
     * 简单转换日期类型到字符串类型,本地信息设为UK
     * 
     * @param date
     * @param format
     * @return String
     */
    public static String getFomartDate(Date date, String format) {
        try {
            return new SimpleDateFormat(format, Locale.UK).format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return (date == null) ? new Date().toString() : date.toString();
        }
    }

    /**
     * @Author: zengkai
     * @Title: getFirstDayOfWeek
     * @Description 获取本周的第一天或最后一天
     * @Date: 2019/7/23 10:59
     * @Param: [today, isFirst: true 表示开始时间,false表示结束时间]
     * @return: java.lang.String
     * @Exception:
     */
    public static String getStartOrEndDayOfWeek(LocalDate today, Boolean isFirst) {
        LocalDate resDate = LocalDate.now();
        if (today == null) {
            today = resDate;
        }
        DayOfWeek week = today.getDayOfWeek();
        int value = week.getValue();
        if (isFirst) {
            resDate = today.minusDays(value - 1);
        } else {
            resDate = today.plusDays(7 - value);
        }
        return resDate.toString();
    }

    /**
     * 功能描述: 获取N分钟之前的时间
     * 
     * @Param: [minute]
     * @Return: java.lang.String
     * @Author: Kai
     * @Date: 2020/11/13 16:41
     */
    public static String getLastDateTimeByMinute(int minute) {
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(Calendar.MINUTE, -minute);
        Date beforeDate = beforeTime.getTime();
        return parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, beforeDate);
    }

    /**
     * 获取指定年份的指定月份数
     * 
     * @return
     */
    public static List getInitMonthToYear(int year, int month) {
        Calendar c = Calendar.getInstance();
        if (month == 0) {
            month = c.get(Calendar.MONTH) + 1;
        }
        List list = getMonth(year, month);
        return list;
    }

    private static List getMonth(int year, int monthNum) {
        Calendar c = Calendar.getInstance();
        List list = new ArrayList<>(monthNum);
        for (int i = 0; i < monthNum; i++) {
            int k = year;
            int j = c.get(Calendar.MONTH) + 1 - i;
            String date;
            if (j >= 1) {
                date = k + "-" + (j >= 10 ? "" : "0") + j;
            } else {
                // 剩余循环次数
                int p = 11 - i;
                int m = c.get(Calendar.YEAR) - 1;
                int n = c.get(Calendar.MONTH) + 2 + p;
                date = m + "-" + (n >= 10 ? "" : "0") + n;
            }
            list.add(date);
        }
        return list;
    }

    /**
     * @Author: umizhang
     * @Title: getStartOrEndDayOfMonth
     * @Description TODO 获取本月的第一天或最后一天
     * @Date: 2019/7/23 11:50
     * @Param: [today, isFirst: true 表示开始时间,false表示结束时间]
     * @return: java.lang.String
     * @Exception:
     */
    public static String getStartOrEndDayOfMonth(LocalDate today, Boolean isFirst) {
        LocalDate resDate = LocalDate.now();
        if (today == null) {
            today = resDate;
        }
        Month month = today.getMonth();
        int length = month.length(today.isLeapYear());
        if (isFirst) {
            resDate = LocalDate.of(today.getYear(), month, 1);
        } else {
            resDate = LocalDate.of(today.getYear(), month, length);
        }
        return resDate.toString();
    }

    /**
     * @Author:
     * @Title: getStartOrEndDayOfQuarter
     * @Description TODO 获取本季度的第一天或最后一天
     * @Date: 2019/7/23 13:46
     * @Param: [today, isFirst: true 表示开始时间,false表示结束时间]
     * @return: java.lang.String
     * @Exception:
     */
    public static String getStartOrEndDayOfQuarter(LocalDate today, Boolean isFirst) {
        LocalDate resDate = LocalDate.now();
        if (today == null) {
            today = resDate;
        }
        Month month = today.getMonth();
        Month firstMonthOfQuarter = month.firstMonthOfQuarter();
        Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
        if (isFirst) {
            resDate = LocalDate.of(today.getYear(), firstMonthOfQuarter, 1);
        } else {
            resDate = LocalDate.of(today.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(today.isLeapYear()));
        }
        return resDate.toString();
    }

    /**
     * @Author: umizhang
     * @Title: getStartOrEndOfYear
     * @Description TODO 获取本年的第一天或最后一天
     * @Date: 2019/7/23 13:48
     * @Param: [today, isFirst: true 表示开始时间,false表示结束时间]
     * @return: java.lang.String
     * @Exception:
     */
    public static String getStartOrEndDayOfYear(LocalDate today, Boolean isFirst) {
        LocalDate resDate = LocalDate.now();
        if (today == null) {
            today = resDate;
        }
        if (isFirst) {
            resDate = LocalDate.of(today.getYear(), Month.JANUARY, 1);
        } else {
            resDate = LocalDate.of(today.getYear(), Month.DECEMBER, Month.DECEMBER.length(today.isLeapYear()));
        }
        return resDate.toString();
    }

    

    /**
     * 得到将date增加指定天数后的date
     * 
     * @param date       日期
     * @param intBetween 增加的天数
     * @return date 加上intBetween天数后的日期
     */
    public static Date increaseDay(Date date, int intBetween) {
        Calendar calo = Calendar.getInstance();
        calo.setTime(date);
        calo.add(Calendar.DATE, intBetween);
        return calo.getTime();
    }

    /**
     * 获取指定日期所在月份开始的时间戳
     * 
     * @param date 指定日期
     * @return
     */
    public static Date getMonthBegin(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        // 设置为1号,当前日期既为本月第一天
        c.set(Calendar.DAY_OF_MONTH, 1);
        // 将小时至0
        c.set(Calendar.HOUR_OF_DAY, 0);
        // 将分钟至0
        c.set(Calendar.MINUTE, 0);
        // 将秒至0
        c.set(Calendar.SECOND, 0);
        // 将毫秒至0
        c.set(Calendar.MILLISECOND, 0);
        // 获取本月第一天的时间戳
        return c.getTime();
    }

    /**
     * 获取指定日期所在月份结束的时间戳
     * 
     * @param date 指定日期
     * @return
     */
    public static Date getMonthEnd(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        // 设置为当月最后一天
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        // 将小时至23
        c.set(Calendar.HOUR_OF_DAY, 23);
        // 将分钟至59
        c.set(Calendar.MINUTE, 59);
        // 将秒至59
        c.set(Calendar.SECOND, 59);
        // 将毫秒至999
        c.set(Calendar.MILLISECOND, 999);
        // 获取本月最后一天的时间戳
        return c.getTime();
    }
    
    /**
     * 获取最近12个月的月份
     * @return list    year-month
     */
    public static List getInitMonthMapWithZero() {
        int monthNum = 12;
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        List list = getMonth(year,monthNum);
        return list;
    }
    /**
     * 返回傳入日期的前或后第n个月的日期,
     * @param date 传入的时间 null则为当前时间
     * @param interval 相隔月份 正数往后,负数往前
     * @param formatString 时间格式
     * @return 传入日期格式的时间字符串
     */
    public static String getDateByMonth(Date date, int interval,String formatString) {
        if(null==date){
            date = getNowDate();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, interval);
        return parseDateToStr(formatString,c.getTime());
    }

    /**
     * 功能描述: 
     * 获取指定日期前后第N个小时的日期
     * @Param: [date, hourNum]
     * @Return: java.lang.String
     * @Author: Kai
     * @Date: 2021/3/20 11:22
     */
    public static String getBeforeHourTime(Date date,int hourNum) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + hourNum);
        return parseDateToStr(YYYY_MM_DD_HH_MM_SS,calendar.getTime());

    }

你可能感兴趣的:(java DateUtils)