All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.keayuan.util.DateFormatUtils Maven / Gradle / Ivy

The newest version!
package cn.keayuan.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public enum DateFormatUtils {
    /**
     * yyyy-MM-dd
     */
    YYYY_MM_DD("yyyy-MM-dd"),
    /**
     * yyyyMMdd
     */
    YYYYMMDD("yyyyMMdd"),
    /**
     * yyyyMM
     */
    YYYYMM("yyyyMM"),
    /**
     * yyyy-MM
     */
    YYYY_MM("yyyy-MM"),
    /**
     * yyyyMMddHHmmss
     */
    YYMDHMS("yyyyMMddHHmmss"),
    /**
     * yyyy-MM-dd HH:mm:ss
     */
    YY_M_D_H_M_S("yyyy-MM-dd HH:mm:ss"),
    /**
     * yyyy-MM-dd HH:mm
     */
    YY_M_D_H_M("yyyy-MM-dd HH:mm"),
    /**
     * yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
     */
    TZ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"),
    /**
     * MM-dd
     */
    MM_DD("MM-dd"),
    /**
     * MM月dd日
     */
    MM_DD2("MM月dd日"),
    /**
     * MM月dd日HH:mm
     */
    MM_DD2_HH_MM("MM月dd日HH:mm"),
    /**
     * yyyy年MM月dd日
     */
    YYYY_MM_DD2("yyyy年MM月dd日"),
    /**
     * yyyy年MM月dd日HH:mm
     */
    YY_M_D_H_M2("yyyy年MM月dd日HH:mm"),
    /**
     * yyyy年MM月dd日 HH:mm:ss
     */
    YY_M_D_H_M_S2("yyyy年MM月dd日 HH:mm:ss"),
    /**
     * KK:mm
     */
    HH_MM_12("KK:mm"),
    /**
     * HH:mm
     */
    HH_MM_24("HH:mm"),
    /**
     * HH:mm:ss
     */
    HH_MM_SS("HH:mm:ss"),

    CUSTOM("");

    private final String format;
    private SimpleDateFormat simpleDateFormat;
    private Date date;

    DateFormatUtils(String format) {
        this.format = format;
    }

    public String format(long time) {
        if (date == null) {
            date = new Date(time);
        }
        date.setTime(time);
        if (simpleDateFormat == null) {
            simpleDateFormat = new SimpleDateFormat(format, Locale.getDefault());
        }
        return simpleDateFormat.format(date);
    }

    public long parse(String date) throws ParseException {
        if (simpleDateFormat == null) {
            simpleDateFormat = new SimpleDateFormat(format, Locale.getDefault());
        }
        Date date1 = simpleDateFormat.parse(date);
        return date1 == null ? 0 : date1.getTime();
    }

    public static synchronized String format(long time, String format) {
        if (CUSTOM.simpleDateFormat == null) {
            CUSTOM.simpleDateFormat = new SimpleDateFormat(format, Locale.getDefault());
        } else {
            CUSTOM.simpleDateFormat.applyPattern(format);
        }
        return CUSTOM.format(time);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy