From 6fe42a2e0a9a7ad12dbd3edae83fb2e09214adb1 Mon Sep 17 00:00:00 2001 From: liquanyu Date: Wed, 26 Jul 2017 16:18:56 +0800 Subject: [PATCH] update code --- .../com/lyms/platform/msg/utils/DateUtils.java | 100 ++++++++++----------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/platform-msg-generate/src/main/java/com/lyms/platform/msg/utils/DateUtils.java b/platform-msg-generate/src/main/java/com/lyms/platform/msg/utils/DateUtils.java index 0fee687..16bc9b7 100644 --- a/platform-msg-generate/src/main/java/com/lyms/platform/msg/utils/DateUtils.java +++ b/platform-msg-generate/src/main/java/com/lyms/platform/msg/utils/DateUtils.java @@ -31,71 +31,71 @@ public class DateUtils { public static final String Y_M_D_H_M_S = "yyyy-MM-dd HH:mm:ss"; public static final String Y_M_D_H_M = "yyyy-MM-dd HH:mm"; -// private static Map> formatMap = new HashMap>(); + private static Map> formatMap = new HashMap>(); - private static ThreadLocal threadLocal = new ThreadLocal(){ - @Override - protected SimpleDateFormat initialValue() { - return new SimpleDateFormat(Y_M_D_H_M_S); - } - }; +// private static ThreadLocal threadLocal = new ThreadLocal(){ +// @Override +// protected SimpleDateFormat initialValue() { +// return new SimpleDateFormat(Y_M_D_H_M_S); +// } +// }; private static final String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; - /** - * 为每个线程创建一个时间格式化实例 - * @param dateFormat - * @return - */ - private static SimpleDateFormat getDateFormat(String dateFormat) - { - SimpleDateFormat simpleDateFormat = threadLocal.get(); - if (simpleDateFormat == null) - { - simpleDateFormat = new SimpleDateFormat(dateFormat); - threadLocal.set(simpleDateFormat); - } - simpleDateFormat.applyPattern(dateFormat); - return simpleDateFormat; - } - - // /** // * 为每个线程创建一个时间格式化实例 // * @param dateFormat // * @return // */ -// private static SimpleDateFormat getDateFormat(final String dateFormat) +// private static SimpleDateFormat getDateFormat(String dateFormat) // { -// try { -// ThreadLocal tl = formatMap.get(dateFormat); -// if (tl == null) -// { -// lock.lock(); -// tl = formatMap.get(dateFormat); -// if (tl == null) -// { -// tl = new ThreadLocal() -// { -// @Override -// protected SimpleDateFormat initialValue() -// { -// return new SimpleDateFormat(dateFormat); -// } -// }; -// formatMap.put(dateFormat, tl); -// } -// } -// return tl.get(); -// } -// finally +// SimpleDateFormat simpleDateFormat = threadLocal.get(); +// if (simpleDateFormat == null) // { -// lock.unlock(); +// simpleDateFormat = new SimpleDateFormat(dateFormat); +// threadLocal.set(simpleDateFormat); // } -// +// simpleDateFormat.applyPattern(dateFormat); +// return simpleDateFormat; // } + + /** + * 为每个线程创建一个时间格式化实例 + * @param dateFormat + * @return + */ + private static SimpleDateFormat getDateFormat(final String dateFormat) + { + try { + ThreadLocal tl = formatMap.get(dateFormat); + if (tl == null) + { + lock.lock(); + tl = formatMap.get(dateFormat); + if (tl == null) + { + tl = new ThreadLocal() + { + @Override + protected SimpleDateFormat initialValue() + { + return new SimpleDateFormat(dateFormat); + } + }; + formatMap.put(dateFormat, tl); + } + } + return tl.get(); + } + finally + { + lock.unlock(); + } + + } + /** * 获取当前日期是星期几
* -- 1.8.3.1