jinpengyong
2021-08-09 1e26fc1ca98b19eaa9813a6ea03a0c65b1ccef39
screen-common/src/main/java/com/moral/util/DateUtils.java
@@ -16,6 +16,10 @@
public class DateUtils {
    /**
     * 日期格式(yyyy)
     */
    public static final String yyyy = "yyyy";
    /**
     * 日期格式(yyyy-MM-dd)
     */
    public static final String yyyy_MM_dd_EN = "yyyy-MM-dd";
@@ -746,8 +750,8 @@
     * @param days
     * @return
     */
    public static String getDateAddDay(String date, int days, String format) {
        DateFormat df = getDateFormat(format);
    public static String getDateAddDay(String date, int days) {
        DateFormat df = getDateFormat(yyyy_MM_dd_EN);
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(df.parse(date));
@@ -761,6 +765,17 @@
    }
    /**
     * 获取当前月第一天
     *
     * @return
     */
    public static Date getFirstDayOfCurrMonth() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, 1);
        return getDate(dateToDateString(cal.getTime(), yyyy_MM_dd_EN));
    }
    /**
     * 获取当前月的最后一天
     *
     * @return
@@ -769,8 +784,7 @@
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, 1);
        cal.set(Calendar.DAY_OF_MONTH, 0);
        return cal.getTime();
        return getDate(dateToDateString(cal.getTime(), yyyy_MM_dd_EN));
    }
    /**
@@ -781,7 +795,7 @@
     * @return
     */
    public static String getDateAddMonth(String date, int m) {
        DateFormat df = getDateFormat(yyyyMM_EN);
        DateFormat df = getDateFormat(yyyy_MM_EN);
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(df.parse(date));
@@ -1312,7 +1326,7 @@
    }
    //获取上周一
    public static Date geLastWeekMonday() {
    public static Date getLastWeekMonday() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDate(getMondayOfThisWeek(), yyyy_MM_dd_EN));
        cal.add(Calendar.DATE, -7);
@@ -1326,4 +1340,52 @@
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return getDate(dateToDateString(calendar.getTime(), yyyy_MM_dd_EN));
    }
    /*获取指定年后年*/
    public static String getDateAddYear(String date, int year) {
        DateFormat df = getDateFormat(yyyy);
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(df.parse(date));
            cal.add(Calendar.YEAR, year);
            date = df.format(cal.getTime());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return date;
    }
    /*
    * 根据时间获取时间内时间点
    * 例:time=2021-08-04 就得到这天内每个小时时间点
    *    time=2021-08 就得到这月内每天时间点
    * */
    public static List<String> getTimeLag(String time) {
        List<String> result = new ArrayList<>();
        int length = time.length();
        Calendar cal = Calendar.getInstance();
        String end;
        String dateFormat;
        int i;
        if (length == 10) {//日
            end = getDateAddDay(time, 1);
            dateFormat = yyyy_MM_dd_EN;
            i = Calendar.HOUR_OF_DAY;
        } else if (length == 7) {//月
            end = getDateAddMonth(time, 1);
            dateFormat = yyyy_MM_EN;
            i = Calendar.DAY_OF_MONTH;
        } else {//年
            end = getDateAddYear(time, 1);
            dateFormat =yyyy;
            i = Calendar.MONTH;
        }
        cal.setTime(getDate(time, dateFormat));
        for (long d = cal.getTimeInMillis(); d < getDate(end, dateFormat).getTime(); cal.set(i, cal.get(i) + 1), d = cal.getTimeInMillis()) {
            String format = dateToDateString(new Date(d));
            result.add(format);
        }
        return result;
    }
}