From b7f85b735debb383899a701fa88c96506dcd0c6b Mon Sep 17 00:00:00 2001 From: liquanyu Date: Wed, 26 Jul 2017 17:50:53 +0800 Subject: [PATCH] update code --- .../com/lyms/platform/msg/utils/DateUtils.java | 87 +++++++--------------- 1 file changed, 27 insertions(+), 60 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 8c2f174..a3a414c 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,65 +31,11 @@ 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 final Object lockObj = new Object(); - - 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 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) - { - ThreadLocal tl = formatMap.get(dateFormat); - if (tl == null) - { - synchronized (lockObj) { - tl = formatMap.get(dateFormat); - if (tl == null) { - tl = new ThreadLocal() { - @Override - protected SimpleDateFormat initialValue() { - return new SimpleDateFormat(dateFormat); - } - }; - formatMap.put(dateFormat, tl); - } - } + private static SimpleDateFormat dateFormat = new SimpleDateFormat(Y_M_D_H_M_S); - } - return tl.get(); - } /** * 获取当前日期是星期几
@@ -126,13 +72,18 @@ public class DateUtils { { try { - return getDateFormat(format).format(date); + lock.lock(); + dateFormat.applyPattern(format); + return dateFormat.format(date); } catch (Exception e) { logger.error("get date format error."); return null; } + finally { + lock.unlock(); + } } @@ -145,8 +96,20 @@ public class DateUtils { */ public static Date formatDate(Date date,String format) { - String time = getDateFormat(format).format(date); - return preaseDate(time, format); + try { + lock.lock(); + dateFormat.applyPattern(format); + String time =dateFormat.format(date); + return dateFormat.parse(time); + } + catch (Exception e) + { + e.printStackTrace(); + } + finally { + lock.unlock(); + } + return null; } /** @@ -159,11 +122,15 @@ public class DateUtils { { try { - Date date = getDateFormat(format).parse(dateStr); - return date; + lock.lock(); + dateFormat.applyPattern(format); + return dateFormat.parse(dateStr); } catch (ParseException e) { e.printStackTrace(); } + finally { + lock.unlock(); + } return null; } -- 1.8.3.1