1 16 package org.mortbay.util; 17 18 import java.text.DateFormatSymbols ; 19 import java.text.SimpleDateFormat ; 20 import java.util.Date ; 21 import java.util.Locale ; 22 import java.util.TimeZone ; 23 24 25 43 44 public class DateCache 45 { 46 private static long __hitWindow=60*60; 47 private static long __MaxMisses=10; 48 49 private String _formatString; 50 private String _tzFormatString; 51 private SimpleDateFormat _tzFormat; 52 53 private String _minFormatString; 54 private SimpleDateFormat _minFormat; 55 56 private String _secFormatString; 57 private String _secFormatString0; 58 private String _secFormatString1; 59 60 private boolean _millis=false; 61 private long _misses = 0; 62 private long _lastMinutes = -1; 63 private long _lastSeconds = -1; 64 private String _lastResult = null; 65 66 private Locale _locale = null; 67 private DateFormatSymbols _dfs = null; 68 69 70 74 public DateCache() 75 { 76 this("EEE MMM dd HH:mm:ss zzz yyyy"); 77 getFormat().setTimeZone(TimeZone.getDefault()); 78 } 79 80 81 84 public DateCache(String format) 85 { 86 _formatString=format; 87 setTimeZone(TimeZone.getDefault()); 88 89 } 90 91 92 public DateCache(String format,Locale l) 93 { 94 _formatString=format; 95 _locale = l; 96 setTimeZone(TimeZone.getDefault()); 97 } 98 99 100 public DateCache(String format,DateFormatSymbols s) 101 { 102 _formatString=format; 103 _dfs = s; 104 setTimeZone(TimeZone.getDefault()); 105 } 106 107 108 111 public void setTimeZone(TimeZone tz) 112 { 113 setTzFormatString(tz); 114 if( _locale != null ) 115 { 116 _tzFormat=new SimpleDateFormat (_tzFormatString,_locale); 117 _minFormat=new SimpleDateFormat (_minFormatString,_locale); 118 } 119 else if( _dfs != null ) 120 { 121 _tzFormat=new SimpleDateFormat (_tzFormatString,_dfs); 122 _minFormat=new SimpleDateFormat (_minFormatString,_dfs); 123 } 124 else 125 { 126 _tzFormat=new SimpleDateFormat (_tzFormatString); 127 _minFormat=new SimpleDateFormat (_minFormatString); 128 } 129 _tzFormat.setTimeZone(tz); 130 _minFormat.setTimeZone(tz); 131 _lastSeconds=-1; 132 _lastMinutes=-1; 133 } 134 135 136 public TimeZone getTimeZone() 137 { 138 return _tzFormat.getTimeZone(); 139 } 140 141 142 146 public void setTimeZoneID(String timeZoneId) 147 { 148 setTimeZone(TimeZone.getTimeZone(timeZoneId)); 149 } 150 151 152 private void setTzFormatString(final TimeZone tz ) 153 { 154 int zIndex = _formatString.indexOf( "ZZZ" ); 155 if( zIndex >= 0 ) 156 { 157 String ss1 = _formatString.substring( 0, zIndex ); 158 String ss2 = _formatString.substring( zIndex+3 ); 159 int tzOffset = tz.getRawOffset(); 160 161 StringBuffer sb = new StringBuffer (_formatString.length()+10); 162 sb.append(ss1); 163 sb.append("'"); 164 if( tzOffset >= 0 ) 165 sb.append( '+' ); 166 else 167 { 168 tzOffset = -tzOffset; 169 sb.append( '-' ); 170 } 171 172 int raw = tzOffset / (1000*60); int hr = raw / 60; 174 int min = raw % 60; 175 176 if( hr < 10 ) 177 sb.append( '0' ); 178 sb.append( hr ); 179 if( min < 10 ) 180 sb.append( '0' ); 181 sb.append( min ); 182 sb.append( '\'' ); 183 184 sb.append(ss2); 185 _tzFormatString=sb.toString(); 186 } 187 else 188 _tzFormatString=_formatString; 189 setMinFormatString(); 190 } 191 192 193 194 private void setMinFormatString() 195 { 196 int i = _tzFormatString.indexOf("ss.SSS"); 197 int l = 6; 198 if (i>=0) 199 _millis=true; 200 else 201 { 202 i = _tzFormatString.indexOf("ss"); 203 l=2; 204 } 205 206 String ss1=_tzFormatString.substring(0,i); 209 String ss2=_tzFormatString.substring(i+l); 210 _minFormatString =ss1+(_millis?"'ss.SSS'":"'ss'")+ss2; 211 } 212 213 214 218 public synchronized String format(Date inDate) 219 { 220 return format(inDate.getTime()); 221 } 222 223 224 228 public synchronized String format(long inDate) 229 { 230 long seconds = inDate / 1000; 231 232 if (seconds<_lastSeconds || 234 _lastSeconds>0 && seconds>_lastSeconds+__hitWindow) 235 { 236 _misses++; 238 if (_misses<__MaxMisses) 239 { 240 Date d = new Date (inDate); 241 return _tzFormat.format(d); 242 } 243 } 244 else if (_misses>0) 245 _misses--; 246 247 if (_lastSeconds==seconds && !_millis) 250 return _lastResult; 251 252 Date d = new Date (inDate); 253 254 long minutes = seconds/60; 256 if (_lastMinutes != minutes) 257 { 258 _lastMinutes = minutes; 259 _secFormatString=_minFormat.format(d); 260 261 int i; 262 int l; 263 if (_millis) 264 { 265 i=_secFormatString.indexOf("ss.SSS"); 266 l=6; 267 } 268 else 269 { 270 i=_secFormatString.indexOf("ss"); 271 l=2; 272 } 273 _secFormatString0=_secFormatString.substring(0,i); 274 _secFormatString1=_secFormatString.substring(i+l); 275 } 276 277 _lastSeconds = seconds; 279 StringBuffer sb=new StringBuffer (_secFormatString.length()); 280 synchronized(sb) 281 { 282 sb.append(_secFormatString0); 283 int s=(int)(seconds%60); 284 if (s<10) 285 sb.append('0'); 286 sb.append(s); 287 if (_millis) 288 { 289 long millis = inDate%1000; 290 if (millis<10) 291 sb.append(".00"); 292 else if (millis<100) 293 sb.append(".0"); 294 else 295 sb.append('.'); 296 sb.append(millis); 297 } 298 sb.append(_secFormatString1); 299 _lastResult=sb.toString(); 300 } 301 302 return _lastResult; 303 } 304 305 306 310 public void format(long inDate, StringBuffer buffer) 311 { 312 buffer.append(format(inDate)); 313 } 314 315 316 318 public SimpleDateFormat getFormat() 319 { 320 return _minFormat; 321 } 322 323 324 public String getFormatString() 325 { 326 return _formatString; 327 } 328 329 } 330 | Popular Tags |