前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >日期格式化工具方法

日期格式化工具方法

作者头像
老梁
发布2019-09-10 18:16:25
9270
发布2019-09-10 18:16:25
举报

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 {
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-02-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 日期格式化工具方法
    • 1.1. 代码
      • 1.2. 说明
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档