1 17 package org.alfresco.util; 18 19 import java.text.ParsePosition ; 20 import java.text.SimpleDateFormat ; 21 import java.util.Date ; 22 import java.util.Locale ; 23 import java.util.Map ; 24 import java.util.WeakHashMap ; 25 26 35 public class CachingDateFormat extends SimpleDateFormat 36 { 37 private static final long serialVersionUID = 3258415049197565235L; 38 39 40 public static final String FORMAT_FULL_GENERIC = "yyyy-MM-dd'T'HH:mm:ss"; 41 42 43 public static final String FORMAT_DATE_GENERIC = "yyyy-MM-dd"; 44 45 46 public static final String FORMAT_TIME_GENERIC = "HH:mm:ss"; 47 48 private static ThreadLocal <SimpleDateFormat > s_localDateFormat = new ThreadLocal <SimpleDateFormat >(); 49 50 private static ThreadLocal <SimpleDateFormat > s_localDateOnlyFormat = new ThreadLocal <SimpleDateFormat >(); 51 52 private static ThreadLocal <SimpleDateFormat > s_localTimeOnlyFormat = new ThreadLocal <SimpleDateFormat >(); 53 54 transient private Map <String , Date > cacheDates = new WeakHashMap <String , Date >(89); 55 56 private CachingDateFormat(String format) 57 { 58 super(format); 59 } 60 61 public String toString() 62 { 63 return this.toPattern(); 64 } 65 66 79 public static SimpleDateFormat getDateFormat(int length, Locale locale, boolean lenient) 80 { 81 SimpleDateFormat dateFormat = (SimpleDateFormat ) CachingDateFormat.getDateInstance(length, locale); 82 String pattern = dateFormat.toPattern(); 84 return getDateFormat(pattern, lenient); 86 } 87 88 103 public static SimpleDateFormat getDateTimeFormat(int dateLength, int timeLength, Locale locale, boolean lenient) 104 { 105 SimpleDateFormat dateFormat = (SimpleDateFormat ) CachingDateFormat.getDateTimeInstance(dateLength, timeLength, locale); 106 String pattern = dateFormat.toPattern(); 108 return getDateFormat(pattern, lenient); 110 } 111 112 121 public static SimpleDateFormat getDateFormat(String pattern, boolean lenient) 122 { 123 SimpleDateFormat dateFormat = new CachingDateFormat(pattern); 125 dateFormat.setLenient(lenient); 127 return dateFormat; 129 } 130 131 136 public static SimpleDateFormat getDateFormat() 137 { 138 if (s_localDateFormat.get() != null) 139 { 140 return s_localDateFormat.get(); 141 } 142 143 CachingDateFormat formatter = new CachingDateFormat(FORMAT_FULL_GENERIC); 144 formatter.setLenient(false); 146 s_localDateFormat.set(formatter); 148 return s_localDateFormat.get(); 150 } 151 152 157 public static SimpleDateFormat getDateOnlyFormat() 158 { 159 if (s_localDateOnlyFormat.get() != null) 160 { 161 return s_localDateOnlyFormat.get(); 162 } 163 164 CachingDateFormat formatter = new CachingDateFormat(FORMAT_DATE_GENERIC); 165 formatter.setLenient(false); 167 s_localDateOnlyFormat.set(formatter); 169 return s_localDateOnlyFormat.get(); 171 } 172 173 178 public static SimpleDateFormat getTimeOnlyFormat() 179 { 180 if (s_localTimeOnlyFormat.get() != null) 181 { 182 return s_localTimeOnlyFormat.get(); 183 } 184 185 CachingDateFormat formatter = new CachingDateFormat(FORMAT_TIME_GENERIC); 186 formatter.setLenient(false); 188 s_localTimeOnlyFormat.set(formatter); 190 return s_localTimeOnlyFormat.get(); 192 } 193 194 200 public Date parse(String text, ParsePosition pos) 201 { 202 Date cached = cacheDates.get(text); 203 if (cached == null) 204 { 205 Date date = super.parse(text, pos); 206 if ((date != null) && (pos.getIndex() == text.length())) 207 { 208 cacheDates.put(text, date); 209 Date clonedDate = (Date ) date.clone(); 210 return clonedDate; 211 } 212 else 213 { 214 return date; 215 } 216 } 217 else 218 { 219 pos.setIndex(text.length()); 220 Date clonedDate = (Date ) cached.clone(); 221 return clonedDate; 222 } 223 } 224 } 225 | Popular Tags |