DateUtils

DateFormat

public enum DateFormat {

    YYMMDD("yyMMdd"),
    YYYYMMDD("yyyyMMdd"),
    YYYY_MM_DD("yyyy-MM-dd"),
    YY_MM_DD("yy-MM-dd"),
    HHMMSS("HHmmss"),
    UNYYMMDD("yy/MM/dd"),
    UNYYYYMMDD("yyyy/MM/dd"),
    HHMMSSS("HHmmssSSS"),
    HH_MM_SS("HH:mm:ss"),
    HH_MM_SSS("HH:mm:ss.SSS"),
    YYHH("yyMMddHHmmss"),
    YYYYHH("yyyyMMddHHmmss"),
    YYYY_HH("yyyy-MM-dd HH:mm:ss"),
    YY_HH("yy-MM-dd HH:mm:ss"),;
    private String val;

    public String getVal() {
        return val;
    }

    public void setVal(String val) {
        this.val = val;
    }

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

DateUtils

public class DateUtils {
    private static ThreadLocal local = new ThreadLocal();

    /*** str to date */
    public static Date format(String date, DateFormat format) {
        if (!StringUtils.isNotBlank(date)) {
            return null;
        }
        SimpleDateFormat sdf = local.get();
        sdf = new SimpleDateFormat(format.getVal(), Locale.US);
        local.set(sdf);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*** date to str  */
    public static String format(Date date, DateFormat format) {
        if (!StringUtils.isNotBlank(date)) {
            return null;
        }
        SimpleDateFormat sdf = local.get();
        sdf = new SimpleDateFormat(format.getVal(), Locale.US);
        local.set(sdf);
        return sdf.format(date);
    }

    /*** 获取当前日期format格式日期字符串 */
    public String getFormat(DateFormat format) {
        return format(new Date(), format);
    }
}

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