Date formatter method

Keywords: Java Oracle

1. Date format tool method

1.1. code

public class DateUtil {
    public enum DateType{
        /**
         * Mm / DD / yyyy HHM / S
         */
        YMDHMS("yyyy-MM-dd HH:mm:ss"),
        /**
         * Mm / DD / yyyy hrs / min
         */
        YMDHM("yyyy-MM-dd HH:mm"),
        /**
         * Specific date
         */
        YMD("yyyy-MM-dd"),
        /**
         * Date: Chinese
         */
        YMD_CN("yyyy year MM month dd day"),
        /**
         * MM DD YY HHM s Chinese
         */
        YMDHMS_CN("yyyy year MM month dd day HH Time mm branch ss second");

        private String format;

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

        public String getFormat() {
            return format;
        }
    }

    public DateUtil() {
    }
    private static Map<String, ThreadLocal<DateFormat>> threadLocalMap = new HashMap<>();



    static {
        DateType[] values = DateType.values();
        for (DateType value : values) {
            String format = value.getFormat();
            threadLocalMap.put(format, ThreadLocal.withInitial(() -> new SimpleDateFormat(format)));
        }
    }

    /**
     * Add a custom date format, preferably during system initialization
     * Date format already exists, check {@ link DateType}
     * @param format Date format, such as yyyy MM DD
     */
    public static void putThreadLocalMap(String format) {
        if (threadLocalMap.get(format) == null) {
            threadLocalMap.put(format, ThreadLocal.withInitial(() -> new SimpleDateFormat(format)));
        }
    }

    public static void main(String[] args) {
        String format = "yyyy/MM/dd";
        putThreadLocalMap(format);
        System.out.println(DateUtil.format(new Date(), DateType.YMDHMS));
        System.out.println(DateUtil.format(new Date(), DateType.YMD));
        System.out.println(DateUtil.format(new Date(), DateType.YMD_CN));
        System.out.println(DateUtil.format(new Date(), format));
    }

    //Date to string
    public static String format(Date date,DateType dateType) {
        String format = dateType.getFormat();
        return format(date, format);
    }

    public static String format(Date date,String format) {
        ThreadLocal<DateFormat> threadLocal = threadLocalMap.get(format);
        if (threadLocal == null) {
            return null;
        }
        return threadLocal.get().format(date);
    }

    //String to date
    public static Date parse(String str,DateType dateType) throws ParseException {
        ThreadLocal<DateFormat> threadLocal = threadLocalMap.get(dateType.getFormat());
        if (threadLocal == null) {
            return null;
        }
        return threadLocal.get().parse(str);
    }
}

1.2. description

                           . Therefore, the more popular way on the Internet is to wrap with ThreadLocal and trade space for time. The above tool class is my own package. If someone can't see it, there is a better alternative or design pattern to put forward

 * <p>
 * Date formats are not synchronized.
 * It is recommended to create separate format instances for each thread.
 * If multiple threads access a format concurrently, it must be synchronized
 * externally.
 * Date format is not synchronized. It is recommended to create a separate format instance for each thread. If multiple threads access a format at the same time, you must     * Externally synchronize the format.
 *
 * @see          <a href="https://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html">Java Tutorial</a>
 * @see          java.util.Calendar
 * @see          java.util.TimeZone
 * @see          DateFormat
 * @see          DateFormatSymbols
 * @author       Mark Davis, Chen-Lieh Huang, Alan Liu
 */
public class SimpleDateFormat extends DateFormat {

Posted by sysop on Mon, 02 Dec 2019 19:05:41 -0800