获取最近n天的日期数组DateUtils

public enum DateFormat {
    ShortNumDate("yyMMdd"),

    NumDate("yyyyMMdd"),

    StrikeDate("yyyy-MM-dd"),

    NumDateTime("yyyyMMddHHmmss"),

    TwoYearNumDateTime("yyMMddHHmmss"),

    StrikeDateTime("yyyy-MM-dd HH:mm:ss"),

    MillisecondTime("yyyy-MM-dd HH:mm:ss SSS"),

    NumTime("HHmmss"),

    ColonTime("HH:mm:ss"),

    LastMonthFirstDay("yyyy-MM-01 00:00:00"),
    Begin("yyyy-MM-dd 00:00:00"),
    End("yyyy-MM-dd 23:59:59"),

    ColonTimeNoSEC("HH:mm");

    private String value;

    DateFormat(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }
}
/**
     * 获取几天前或几天后的某天日期
     */
    public static String getBeforeOrAfterDay(boolean before, Integer day, DateFormat dateFormat) {
        Calendar calendar = Calendar.getInstance();
        if (before) {
            calendar.add(Calendar.DATE, -day);
        } else {
            calendar.add(Calendar.DATE, day);
        }
        SimpleDateFormat format = new SimpleDateFormat(dateFormat.getValue());
        return format.format(calendar.getTime());
    }

    /**
     * 获取最近n天的日期数组
     */
    public static List getLastDays(Integer day, DateFormat dateFormat) {
        List list = new ArrayList<>();
        for (int i = 0; i < day; i++) {
            list.add(getBeforeOrAfterDay(true, i, dateFormat));
        }
        return list;
    }

你可能感兴趣的:(工具类系列)