1 16 package com.blandware.atleap.webapp.util.core; 17 18 import java.util.Date ; 19 import java.util.HashMap ; 20 import java.util.Locale ; 21 import java.util.TimeZone ; 22 import java.text.DateFormat ; 23 import java.text.ParseException ; 24 import java.text.SimpleDateFormat ; 25 26 35 public class FastHttpDateFormat { 36 37 39 40 43 protected static final SimpleDateFormat format = 44 new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US); 45 46 47 50 protected static final SimpleDateFormat formats[] = { 51 new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US), 52 new SimpleDateFormat ("EEEEEE, dd-MMM-yy HH:mm:ss zzz", Locale.US), 53 new SimpleDateFormat ("EEE MMMM d HH:mm:ss yyyy", Locale.US) 54 }; 55 56 57 60 protected final static TimeZone gmtZone = TimeZone.getTimeZone("GMT"); 61 62 63 static { 64 65 format.setTimeZone(gmtZone); 66 67 formats[0].setTimeZone(gmtZone); 68 formats[1].setTimeZone(gmtZone); 69 formats[2].setTimeZone(gmtZone); 70 71 } 72 73 74 77 protected static long currentDateGenerated = 0L; 78 79 80 83 protected static String currentDate = null; 84 85 86 89 protected static final HashMap formatCache = new HashMap (); 90 91 92 95 protected static final HashMap parseCache = new HashMap (); 96 97 98 100 101 106 public static final String getCurrentDate() { 107 108 long now = System.currentTimeMillis(); 109 if ((now - currentDateGenerated) > 1000) { 110 synchronized (format) { 111 if ((now - currentDateGenerated) > 1000) { 112 currentDateGenerated = now; 113 currentDate = format.format(new Date (now)); 114 } 115 } 116 } 117 return currentDate; 118 119 } 120 121 122 131 public static final String formatDate(long value, 132 DateFormat threadLocalformat) { 133 134 String cachedDate = null; 135 Long longValue = new Long (value); 136 try { 137 cachedDate = (String ) formatCache.get(longValue); 138 } catch (Exception e) { 139 } 140 if (cachedDate != null) 141 return cachedDate; 142 143 String newDate = null; 144 Date dateValue = new Date (value); 145 if (threadLocalformat != null) { 146 newDate = threadLocalformat.format(dateValue); 147 synchronized (formatCache) { 148 updateCache(formatCache, longValue, newDate); 149 } 150 } else { 151 synchronized (formatCache) { 152 newDate = format.format(dateValue); 153 updateCache(formatCache, longValue, newDate); 154 } 155 } 156 return newDate; 157 158 } 159 160 161 170 public static final long parseDate(String value, 171 DateFormat [] threadLocalformats) { 172 173 Long cachedDate = null; 174 try { 175 cachedDate = (Long ) parseCache.get(value); 176 } catch (Exception e) { 177 } 178 if (cachedDate != null) 179 return cachedDate.longValue(); 180 181 Long date = null; 182 if (threadLocalformats != null) { 183 date = internalParseDate(value, threadLocalformats); 184 synchronized (parseCache) { 185 updateCache(parseCache, value, date); 186 } 187 } else { 188 synchronized (parseCache) { 189 date = internalParseDate(value, formats); 190 updateCache(parseCache, value, date); 191 } 192 } 193 if (date == null) { 194 return (-1L); 195 } else { 196 return date.longValue(); 197 } 198 199 } 200 201 202 209 private static final Long internalParseDate 210 (String value, DateFormat [] formats) { 211 Date date = null; 212 for (int i = 0; (date == null) && (i < formats.length); i++) { 213 try { 214 date = formats[i].parse(value); 215 } catch (ParseException e) { 216 ; 217 } 218 } 219 if (date == null) { 220 return null; 221 } 222 return new Long (date.getTime()); 223 } 224 225 226 233 private static final void updateCache(HashMap cache, Object key, 234 Object value) { 235 if (value == null) { 236 return; 237 } 238 if (cache.size() > 1000) { 239 cache.clear(); 240 } 241 cache.put(key, value); 242 } 243 } 244 | Popular Tags |