zoukankan      html  css  js  c++  java
  • 日期格式化工具方法

    1. 日期格式化工具方法

    1.1. 代码

    public class DateUtil {
        public enum DateType{
            /**
             * 年月日时分秒
             */
            YMDHMS("yyyy-MM-dd HH:mm:ss"),
            /**
             * 年月日时分
             */
            YMDHM("yyyy-MM-dd HH:mm"),
            /**
             * 年月日
             */
            YMD("yyyy-MM-dd"),
            /**
             * 年月日中文
             */
            YMD_CN("yyyy年MM月dd日"),
            /**
             * 年月日时分秒中文
             */
            YMDHMS_CN("yyyy年MM月dd日 HH时mm分ss秒");
    
            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)));
            }
        }
    
        /**
         * 添加自定义日期格式,最好在系统初始化的时候加进去
         * 已存在日期格式,查看{@link DateType}
         * @param format 日期格式,例如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));
        }
    
        //日期转字符串
        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);
        }
    
        //字符串转日期
        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. 说明

      以上是基于jdk8语法实现,但格式化工具还是用的SimpleDateFormat,该类通过查看它的注解,可以知道它并不是线程安全的,但是每次单独实例化它也是比较耗费资源的。因此网上较流行的方式就是用ThreadLocal包裹,用空间换时间的方式,上述工具类为我自己的封装,如果有人看不下去的,有更好的替代方案或设计模式可以提出来哦

     * <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.
     * 日期格式没有同步。建议为每个线程创建单独的格式实例。如果多个线程同时访问一种格式,则必须在	 * 外部同步该格式。
     *
     * @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 {
    
  • 相关阅读:
    SQL分类
    简单poi读取excel
    Linux命令(2)-rm删除文件
    Linux下使用yum安装MariaDB
    linux下vi命令
    等价类划分法设计测试用例
    Linux命令(1)-创建文件
    职场面试必知:如何回答为何离开上一家公司
    软件测试的原则
    JAVA数组去除重复数据
  • 原文地址:https://www.cnblogs.com/sky-chen/p/10399791.html
Copyright © 2011-2022 走看看