时间转换封装 线程安全
来源:互联网 发布:用mac整理iphone照片 编辑:程序博客网 时间:2024/04/30 07:35
为了有一个公用的时间处理工具,就封装了一个,该封装的工具是线程安全的,可以放心使用。
public class SafeDateFormat { final static Map<String, ThreadLocal<DateFormat>> threadLocalPool = new HashMap<>(); final static ThreadLocal<DateFormat> DefaultThreadLocal = new ThreadLocal<DateFormat>() { @Override protected synchronized DateFormat initialValue() { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); } }; static { threadLocalPool.put("yyyy-MM-dd HH:mm:ss", DefaultThreadLocal); } public static Date parse(String dateStr) throws ParseException { return DefaultThreadLocal.get().parse(dateStr); } public static Date parse(final String format, String dateStr) throws ParseException { ThreadLocal<DateFormat> threadLocal = threadLocalPool.get(format); if (threadLocal == null) { threadLocal = new ThreadLocal<DateFormat>() { @Override protected synchronized DateFormat initialValue() { return new SimpleDateFormat(format); } }; threadLocalPool.put(format, threadLocal); } return threadLocal.get().parse(dateStr); } public static String format(Date date) { return DefaultThreadLocal.get().format(date); } public static String format(final String format, Date date) { ThreadLocal<DateFormat> threadLocal = threadLocalPool.get(format); if (threadLocal == null) { threadLocal = new ThreadLocal<DateFormat>() { @Override protected synchronized DateFormat initialValue() { return new SimpleDateFormat(format); } }; threadLocalPool.put(format, threadLocal); } return threadLocal.get().format(date); }
0 0
- 时间转换封装 线程安全
- 线程安全的时间类
- 一个线程安全的std::map封装
- 各种封装好的时间转换
- js封装时间转换工具,实现日期之前的转换
- 封装多媒体定时器类和时间核心对象线程类
- java8中将非线程安全类采用lamda表达式转换成线程安全类的方式
- 利用Collections将非线程安全的Map转换成线程安全的Map
- 关于SimpleDateFormat安全的时间格式化线程安全问题
- linux中关于时间函数的线程安全
- 关于SimpleDateFormat安全的时间格式化线程安全问题
- 关于SimpleDateFormat安全的时间格式化线程安全问题
- 关于SimpleDateFormat安全的时间格式化线程安全问题
- 关于SimpleDateFormat安全的时间格式化线程安全问题
- 关于SimpleDateFormat安全的时间格式化线程安全问题
- 关于SimpleDateFormat安全的时间格式化线程安全问题
- 4.7---linux修改系统时间---集合线程安全---guava cache
- 关于SimpleDateFormat安全的时间格式化线程安全问题
- HDOJ 1096 A+B for Input-Output Practice (VIII)
- HDOJ 1042 N!
- Intellij Idea 主题下载(Eclectide Monokai)
- 深入理解Java内部类
- 私用 Hadoop BigTable
- 时间转换封装 线程安全
- OC中 新建对象 new 和alloc的区别
- C# 表复制和数据行的复制说明(Clone、ImportRow 、Copy )
- OC中 新建对象 new 和alloc的区别
- git reset用法
- android sqlite3的使用
- 4.zabbix学习笔记:zabbix深入配置和使用 之 Items
- nutch解析网页时NoClassDefFoundError异常解决方法
- Struts2——No result defined for action and result input - action错误