Commit b7f85b735debb383899a701fa88c96506dcd0c6b
1 parent
a06bd9c38e
Exists in
master
and in
6 other branches
update code
Showing 1 changed file with 27 additions and 60 deletions
platform-msg-generate/src/main/java/com/lyms/platform/msg/utils/DateUtils.java
View file @
b7f85b7
| ... | ... | @@ -31,67 +31,13 @@ |
| 31 | 31 | public static final String Y_M_D_H_M_S = "yyyy-MM-dd HH:mm:ss"; |
| 32 | 32 | public static final String Y_M_D_H_M = "yyyy-MM-dd HH:mm"; |
| 33 | 33 | |
| 34 | - /** 锁对象 */ | |
| 35 | - private static final Object lockObj = new Object(); | |
| 36 | - | |
| 37 | - private static Map<String, ThreadLocal<SimpleDateFormat>> formatMap = new HashMap<String, ThreadLocal<SimpleDateFormat>>(); | |
| 38 | - | |
| 39 | -// private static ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<SimpleDateFormat>(){ | |
| 40 | -// @Override | |
| 41 | -// protected SimpleDateFormat initialValue() { | |
| 42 | -// return new SimpleDateFormat(Y_M_D_H_M_S); | |
| 43 | -// } | |
| 44 | -// }; | |
| 45 | - | |
| 46 | 34 | private static final String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; |
| 47 | 35 | |
| 48 | 36 | |
| 49 | -// /** | |
| 50 | -// * 为每个线程创建一个时间格式化实例 | |
| 51 | -// * @param dateFormat | |
| 52 | -// * @return | |
| 53 | -// */ | |
| 54 | -// private static SimpleDateFormat getDateFormat(String dateFormat) | |
| 55 | -// { | |
| 56 | -// SimpleDateFormat simpleDateFormat = threadLocal.get(); | |
| 57 | -// if (simpleDateFormat == null) | |
| 58 | -// { | |
| 59 | -// simpleDateFormat = new SimpleDateFormat(dateFormat); | |
| 60 | -// threadLocal.set(simpleDateFormat); | |
| 61 | -// } | |
| 62 | -// simpleDateFormat.applyPattern(dateFormat); | |
| 63 | -// return simpleDateFormat; | |
| 64 | -// } | |
| 37 | + private static SimpleDateFormat dateFormat = new SimpleDateFormat(Y_M_D_H_M_S); | |
| 65 | 38 | |
| 66 | 39 | |
| 67 | 40 | /** |
| 68 | - * 为每个线程创建一个时间格式化实例 | |
| 69 | - * @param dateFormat | |
| 70 | - * @return | |
| 71 | - */ | |
| 72 | - private static SimpleDateFormat getDateFormat(final String dateFormat) | |
| 73 | - { | |
| 74 | - ThreadLocal<SimpleDateFormat> tl = formatMap.get(dateFormat); | |
| 75 | - if (tl == null) | |
| 76 | - { | |
| 77 | - synchronized (lockObj) { | |
| 78 | - tl = formatMap.get(dateFormat); | |
| 79 | - if (tl == null) { | |
| 80 | - tl = new ThreadLocal<SimpleDateFormat>() { | |
| 81 | - @Override | |
| 82 | - protected SimpleDateFormat initialValue() { | |
| 83 | - return new SimpleDateFormat(dateFormat); | |
| 84 | - } | |
| 85 | - }; | |
| 86 | - formatMap.put(dateFormat, tl); | |
| 87 | - } | |
| 88 | - } | |
| 89 | - | |
| 90 | - } | |
| 91 | - return tl.get(); | |
| 92 | - } | |
| 93 | - | |
| 94 | - /** | |
| 95 | 41 | * 获取当前日期是星期几<br> |
| 96 | 42 | * |
| 97 | 43 | * @param dt |
| 98 | 44 | |
| ... | ... | @@ -126,13 +72,18 @@ |
| 126 | 72 | { |
| 127 | 73 | try |
| 128 | 74 | { |
| 129 | - return getDateFormat(format).format(date); | |
| 75 | + lock.lock(); | |
| 76 | + dateFormat.applyPattern(format); | |
| 77 | + return dateFormat.format(date); | |
| 130 | 78 | } |
| 131 | 79 | catch (Exception e) |
| 132 | 80 | { |
| 133 | 81 | logger.error("get date format error."); |
| 134 | 82 | return null; |
| 135 | 83 | } |
| 84 | + finally { | |
| 85 | + lock.unlock(); | |
| 86 | + } | |
| 136 | 87 | |
| 137 | 88 | } |
| 138 | 89 | |
| ... | ... | @@ -145,8 +96,20 @@ |
| 145 | 96 | */ |
| 146 | 97 | public static Date formatDate(Date date,String format) |
| 147 | 98 | { |
| 148 | - String time = getDateFormat(format).format(date); | |
| 149 | - return preaseDate(time, format); | |
| 99 | + try { | |
| 100 | + lock.lock(); | |
| 101 | + dateFormat.applyPattern(format); | |
| 102 | + String time =dateFormat.format(date); | |
| 103 | + return dateFormat.parse(time); | |
| 104 | + } | |
| 105 | + catch (Exception e) | |
| 106 | + { | |
| 107 | + e.printStackTrace(); | |
| 108 | + } | |
| 109 | + finally { | |
| 110 | + lock.unlock(); | |
| 111 | + } | |
| 112 | + return null; | |
| 150 | 113 | } |
| 151 | 114 | |
| 152 | 115 | /** |
| 153 | 116 | |
| ... | ... | @@ -159,10 +122,14 @@ |
| 159 | 122 | { |
| 160 | 123 | try |
| 161 | 124 | { |
| 162 | - Date date = getDateFormat(format).parse(dateStr); | |
| 163 | - return date; | |
| 125 | + lock.lock(); | |
| 126 | + dateFormat.applyPattern(format); | |
| 127 | + return dateFormat.parse(dateStr); | |
| 164 | 128 | } catch (ParseException e) { |
| 165 | 129 | e.printStackTrace(); |
| 130 | + } | |
| 131 | + finally { | |
| 132 | + lock.unlock(); | |
| 166 | 133 | } |
| 167 | 134 | return null; |
| 168 | 135 | } |