1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.util.date;
35
36 import fr.paris.lutece.portal.service.i18n.I18nService;
37 import fr.paris.lutece.portal.web.l10n.LocaleService;
38
39 import java.sql.Timestamp;
40
41 import java.text.DateFormat;
42 import java.text.FieldPosition;
43 import java.text.ParseException;
44 import java.text.ParsePosition;
45 import java.text.SimpleDateFormat;
46
47 import java.util.Date;
48 import java.util.Locale;
49
50
51
52
53
54 public final class DateUtil
55 {
56 @Deprecated
57 private static SimpleDateFormat _formatter = new SimpleDateFormat( "dd'/'MM'/'yyyy", Locale.FRANCE );
58 @Deprecated
59 private static SimpleDateFormat _formatterDateTime = new SimpleDateFormat( "dd'/'MM'/'yyyy' 'HH':'mm", Locale.FRANCE );
60 private static final String CONSTANTE_PATTERN_DATE = "dd/MM/yyyy";
61 private static final long CONSTANT_NUMBER_MILISECONDS_IN_DAY = 86400000;
62
63
64
65
66 private DateUtil( )
67 {
68 }
69
70
71
72
73
74
75
76 public static synchronized String getCurrentDateString( )
77 {
78 return _formatter.format( new java.util.Date( ) );
79 }
80
81
82
83
84
85
86
87
88 public static synchronized java.sql.Date getDateSql( String strDate )
89 {
90 ParsePosition pos = new ParsePosition( 0 );
91 java.util.Date date = _formatter.parse( strDate, pos );
92
93 if ( date != null )
94 {
95 return new java.sql.Date( date.getTime( ) );
96 }
97
98 return null;
99 }
100
101
102
103
104
105
106
107
108 public static synchronized java.util.Date getDate( String strDate )
109 {
110 ParsePosition pos = new ParsePosition( 0 );
111 java.util.Date date = _formatter.parse( strDate, pos );
112
113 return date;
114 }
115
116
117
118
119
120
121
122
123 public static synchronized java.sql.Timestamp getTimestamp( String strDate )
124 {
125 ParsePosition pos = new ParsePosition( 0 );
126 java.util.Date date = _formatter.parse( strDate, pos );
127
128 if ( date != null )
129 {
130 return ( new java.sql.Timestamp( date.getTime( ) ) );
131 }
132
133 return null;
134 }
135
136
137
138
139
140
141
142
143 public static synchronized String getDateString( java.sql.Date date )
144 {
145 if ( date != null )
146 {
147 StringBuffer strDate = new StringBuffer( );
148 _formatter.format( date, strDate, new FieldPosition( 0 ) );
149
150 return strDate.toString( );
151 }
152
153 return "";
154 }
155
156
157
158
159
160
161
162
163
164
165
166 public static synchronized String getDateString( java.sql.Timestamp date )
167 {
168 if ( date != null )
169 {
170 StringBuffer strDate = new StringBuffer( );
171 _formatter.format( date, strDate, new FieldPosition( 0 ) );
172
173 return strDate.toString( );
174 }
175
176 return "";
177 }
178
179
180
181
182
183
184
185
186
187
188
189 public static synchronized String getDateString( java.util.Date date )
190 {
191 if ( date != null )
192 {
193 StringBuffer strDate = new StringBuffer( );
194 _formatter.format( date, strDate, new FieldPosition( 0 ) );
195
196 return strDate.toString( );
197 }
198
199 return "";
200 }
201
202
203
204
205
206
207
208
209
210
211
212 public static synchronized String getDateTimeString( long lTime )
213 {
214 StringBuffer strDate = new StringBuffer( );
215 _formatterDateTime.format( new java.util.Date( lTime ), strDate, new FieldPosition( 0 ) );
216
217 return strDate.toString( );
218 }
219
220
221
222
223
224
225
226
227
228
229 public static Date formatDate( String strDate, Locale locale )
230 {
231 Date date = null;
232
233 if ( strDate != null )
234 {
235 DateFormat dateFormat = null;
236
237 if ( locale != null )
238 {
239 String strLocalizedDateFormat = I18nService.getDateFormatShortPattern( locale );
240
241 if ( ( strLocalizedDateFormat != null ) && !strLocalizedDateFormat.equals( "" ) )
242 {
243 dateFormat = new SimpleDateFormat( strLocalizedDateFormat );
244 }
245 else
246 {
247 dateFormat = DateFormat.getDateInstance( DateFormat.SHORT, locale );
248 }
249 }
250 else
251 {
252 dateFormat = DateFormat.getDateInstance( DateFormat.SHORT, LocaleService.getDefault( ) );
253 }
254
255 dateFormat.setLenient( false );
256
257 try
258 {
259 date = dateFormat.parse( strDate );
260 }
261 catch ( ParseException e )
262 {
263 return null;
264 }
265 }
266
267 return date;
268 }
269
270
271
272
273
274
275
276 public static Date formatDateLongYear( String strDate, Locale locale )
277 {
278 Date date = null;
279
280 if ( ( strDate != null ) && ( strDate.trim( ).length( ) == CONSTANTE_PATTERN_DATE.length( ) ) )
281 {
282 date = formatDate( strDate, locale );
283 }
284
285 return date;
286 }
287
288
289
290
291
292
293
294
295 public static Timestamp formatTimestamp( String strDate, Locale locale )
296 {
297 Date date = formatDate( strDate, locale );
298
299 if ( date == null )
300 {
301 return null;
302 }
303
304 return new Timestamp( date.getTime( ) );
305 }
306
307
308
309
310
311
312
313
314 public static java.sql.Date formatDateSql( String strDate, Locale locale )
315 {
316 Date date = formatDate( strDate, locale );
317
318 if ( date == null )
319 {
320 return null;
321 }
322
323 return new java.sql.Date( date.getTime( ) );
324 }
325
326
327
328
329
330
331
332 public static String getDateString( Date date, Locale locale )
333 {
334 DateFormat dateFormat = null;
335
336 if ( locale != null )
337 {
338 String strLocalizedDateFormat = I18nService.getDateFormatShortPattern( locale );
339
340 if ( ( strLocalizedDateFormat != null ) && !strLocalizedDateFormat.equals( "" ) )
341 {
342 dateFormat = new SimpleDateFormat( strLocalizedDateFormat );
343 }
344 else
345 {
346 dateFormat = DateFormat.getDateInstance( DateFormat.SHORT, locale );
347 }
348 }
349 else
350 {
351 dateFormat = DateFormat.getDateInstance( DateFormat.SHORT, LocaleService.getDefault( ) );
352 }
353
354 return dateFormat.format( date );
355 }
356
357
358
359
360
361
362
363
364 public static String getDateString( long lTime, Locale locale )
365 {
366 return getDateString( new Date( lTime ), locale );
367 }
368
369
370
371
372
373
374
375
376 public static String getDateString( java.sql.Timestamp date, Locale locale )
377 {
378 if ( date == null )
379 {
380 return "";
381 }
382
383 return getDateString( new Date( date.getTime( ) ), locale );
384 }
385
386
387
388
389
390
391
392 public static String getCurrentDateString( Locale locale )
393 {
394 return getDateString( new Date( ), locale );
395 }
396
397
398
399
400
401
402 public static String getDefaultPattern( Locale locale )
403 {
404 if ( locale != null )
405 {
406 String strLocalizedDateFormat = I18nService.getDateFormatShortPattern( locale );
407
408 if ( ( strLocalizedDateFormat != null ) && !strLocalizedDateFormat.equals( "" ) )
409 {
410 return strLocalizedDateFormat;
411 }
412
413 DateFormat df = DateFormat.getDateInstance( DateFormat.SHORT, locale );
414
415 if ( df instanceof SimpleDateFormat )
416 {
417 SimpleDateFormat sdf = (SimpleDateFormat) df;
418
419 return sdf.toPattern( );
420 }
421 }
422
423 DateFormat df = DateFormat.getDateInstance( DateFormat.SHORT, LocaleService.getDefault( ) );
424
425 if ( df instanceof SimpleDateFormat )
426 {
427 SimpleDateFormat sdf = (SimpleDateFormat) df;
428
429 return sdf.toPattern( );
430 }
431
432 return null;
433 }
434
435
436
437
438
439
440 public static long convertDaysInMiliseconds( long lDays )
441 {
442 return CONSTANT_NUMBER_MILISECONDS_IN_DAY * lDays;
443 }
444 }