1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.util;
20
21 import java.text.SimpleDateFormat;
22 import java.util.Date;
23 import java.util.Locale;
24 import java.util.TimeZone;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class DateCache
44 {
45 public static final String DEFAULT_FORMAT="EEE MMM dd HH:mm:ss zzz yyyy";
46
47 private final String _formatString;
48 private final String _tzFormatString;
49 private final SimpleDateFormat _tzFormat;
50 private final Locale _locale ;
51
52 private volatile Tick _tick;
53
54
55
56 public static class Tick
57 {
58 final long _seconds;
59 final String _string;
60 public Tick(long seconds, String string)
61 {
62 _seconds = seconds;
63 _string = string;
64 }
65 }
66
67
68
69
70
71
72 public DateCache()
73 {
74 this(DEFAULT_FORMAT);
75 }
76
77
78
79
80
81 public DateCache(String format)
82 {
83 this(format,null,TimeZone.getDefault());
84 }
85
86
87 public DateCache(String format,Locale l)
88 {
89 this(format,l,TimeZone.getDefault());
90 }
91
92
93 public DateCache(String format,Locale l,String tz)
94 {
95 this(format,l,TimeZone.getTimeZone(tz));
96 }
97
98
99 public DateCache(String format,Locale l,TimeZone tz)
100 {
101 _formatString=format;
102 _locale = l;
103
104
105 int zIndex = _formatString.indexOf( "ZZZ" );
106 if( zIndex >= 0 )
107 {
108 String ss1 = _formatString.substring( 0, zIndex );
109 String ss2 = _formatString.substring( zIndex+3 );
110 int tzOffset = tz.getRawOffset();
111
112 StringBuilder sb = new StringBuilder(_formatString.length()+10);
113 sb.append(ss1);
114 sb.append("'");
115 if( tzOffset >= 0 )
116 sb.append( '+' );
117 else
118 {
119 tzOffset = -tzOffset;
120 sb.append( '-' );
121 }
122
123 int raw = tzOffset / (1000*60);
124 int hr = raw / 60;
125 int min = raw % 60;
126
127 if( hr < 10 )
128 sb.append( '0' );
129 sb.append( hr );
130 if( min < 10 )
131 sb.append( '0' );
132 sb.append( min );
133 sb.append( '\'' );
134
135 sb.append(ss2);
136 _tzFormatString=sb.toString();
137 }
138 else
139 _tzFormatString=_formatString;
140
141 if( _locale != null )
142 {
143 _tzFormat=new SimpleDateFormat(_tzFormatString,_locale);
144 }
145 else
146 {
147 _tzFormat=new SimpleDateFormat(_tzFormatString);
148 }
149 _tzFormat.setTimeZone(tz);
150
151 _tick=null;
152 }
153
154
155
156 public TimeZone getTimeZone()
157 {
158 return _tzFormat.getTimeZone();
159 }
160
161
162
163
164
165
166
167 public String format(Date inDate)
168 {
169 long seconds = inDate.getTime() / 1000;
170
171 Tick tick=_tick;
172
173
174 if (tick==null || seconds!=tick._seconds)
175 {
176
177 synchronized (this)
178 {
179 return _tzFormat.format(inDate);
180 }
181 }
182
183 return tick._string;
184 }
185
186
187
188
189
190
191
192
193 public String format(long inDate)
194 {
195 long seconds = inDate / 1000;
196
197 Tick tick=_tick;
198
199
200 if (tick==null || seconds!=tick._seconds)
201 {
202
203 Date d = new Date(inDate);
204 synchronized (this)
205 {
206 return _tzFormat.format(d);
207 }
208 }
209
210 return tick._string;
211 }
212
213
214
215
216
217
218
219
220
221 public String formatNow(long now)
222 {
223 long seconds = now / 1000;
224
225 Tick tick=_tick;
226
227
228 if (tick!=null && tick._seconds==seconds)
229 return tick._string;
230 return formatTick(now)._string;
231 }
232
233
234 public String now()
235 {
236 return formatNow(System.currentTimeMillis());
237 }
238
239
240 public Tick tick()
241 {
242 return formatTick(System.currentTimeMillis());
243 }
244
245
246 protected Tick formatTick(long now)
247 {
248 long seconds = now / 1000;
249
250
251 synchronized (this)
252 {
253
254 if (_tick==null || _tick._seconds!=seconds)
255 {
256 String s= _tzFormat.format(new Date(now));
257 return _tick=new Tick(seconds,s);
258 }
259 return _tick;
260 }
261 }
262
263
264 public String getFormatString()
265 {
266 return _formatString;
267 }
268 }