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 java.sql.Timestamp;
37 import java.text.DateFormat;
38 import java.text.ParseException;
39 import java.text.SimpleDateFormat;
40 import java.util.Date;
41 import java.util.Locale;
42
43 import org.apache.commons.lang3.StringUtils;
44
45 import fr.paris.lutece.portal.service.i18n.I18nService;
46 import fr.paris.lutece.portal.web.l10n.LocaleService;
47
48
49
50
51 public final class DateUtil
52 {
53 public static final String ISO_PATTERN_DATE = "yyyy-MM-dd HH:mm:ss";
54 private static final String CONSTANTE_PATTERN_DATE = "dd/MM/yyyy";
55 private static final long CONSTANT_NUMBER_MILISECONDS_IN_DAY = 86400000;
56
57
58
59
60 private DateUtil( )
61 {
62 }
63
64
65
66
67
68
69
70
71
72
73 public static Date formatDate( String strDate, Locale locale )
74 {
75 Date date = null;
76
77 if ( strDate != null )
78 {
79 DateFormat dateFormat = getDateFormat( locale );
80 try
81 {
82 date = dateFormat.parse( strDate );
83 }
84 catch( ParseException e )
85 {
86 return null;
87 }
88 }
89
90 return date;
91 }
92
93
94
95
96
97
98
99
100
101
102 public static Date formatDateLongYear( String strDate, Locale locale )
103 {
104 Date date = null;
105
106 if ( ( strDate != null ) && ( strDate.trim( ).length( ) == CONSTANTE_PATTERN_DATE.length( ) ) )
107 {
108 date = formatDate( strDate, locale );
109 }
110
111 return date;
112 }
113
114
115
116
117
118
119
120
121
122
123 public static Timestamp formatTimestamp( String strDate, Locale locale )
124 {
125 Date date = formatDate( strDate, locale );
126
127 if ( date == null )
128 {
129 return null;
130 }
131
132 return new Timestamp( date.getTime( ) );
133 }
134
135
136
137
138
139
140
141
142
143
144 public static java.sql.Date formatDateSql( String strDate, Locale locale )
145 {
146 Date date = formatDate( strDate, locale );
147
148 if ( date == null )
149 {
150 return null;
151 }
152
153 return new java.sql.Date( date.getTime( ) );
154 }
155
156
157
158
159
160
161
162
163
164
165 public static String getDateString( Date date, Locale locale )
166 {
167 DateFormat dateFormat = null;
168
169 if ( locale != null )
170 {
171 String strLocalizedDateFormat = I18nService.getDateFormatShortPattern( locale );
172
173 if ( ( strLocalizedDateFormat != null ) && !strLocalizedDateFormat.equals( "" ) )
174 {
175 dateFormat = new SimpleDateFormat( strLocalizedDateFormat );
176 }
177 else
178 {
179 dateFormat = DateFormat.getDateInstance( DateFormat.SHORT, locale );
180 }
181 }
182 else
183 {
184 dateFormat = DateFormat.getDateInstance( DateFormat.SHORT, LocaleService.getDefault( ) );
185 }
186
187 return dateFormat.format( date );
188 }
189
190
191
192
193
194
195
196
197
198
199 public static String getDateString( long lTime, Locale locale )
200 {
201 return getDateString( new Date( lTime ), locale );
202 }
203
204
205
206
207
208
209
210
211
212
213 public static String getDateString( java.sql.Timestamp date, Locale locale )
214 {
215 if ( date == null )
216 {
217 return "";
218 }
219
220 return getDateString( new Date( date.getTime( ) ), locale );
221 }
222
223
224
225
226
227
228
229
230 public static String getCurrentDateString( Locale locale )
231 {
232 return getDateString( new Date( ), locale );
233 }
234
235
236
237
238
239
240
241
242 public static String getDefaultPattern( Locale locale )
243 {
244 if ( locale != null )
245 {
246 String strLocalizedDateFormat = I18nService.getDateFormatShortPattern( locale );
247
248 if ( ( strLocalizedDateFormat != null ) && !strLocalizedDateFormat.equals( "" ) )
249 {
250 return strLocalizedDateFormat;
251 }
252
253 DateFormat df = DateFormat.getDateInstance( DateFormat.SHORT, locale );
254
255 if ( df instanceof SimpleDateFormat )
256 {
257 SimpleDateFormat sdf = (SimpleDateFormat) df;
258
259 return sdf.toPattern( );
260 }
261 }
262
263 DateFormat df = DateFormat.getDateInstance( DateFormat.SHORT, LocaleService.getDefault( ) );
264
265 if ( df instanceof SimpleDateFormat )
266 {
267 SimpleDateFormat sdf = (SimpleDateFormat) df;
268
269 return sdf.toPattern( );
270 }
271
272 return null;
273 }
274
275
276
277
278
279
280
281
282 public static long convertDaysInMiliseconds( long lDays )
283 {
284 return CONSTANT_NUMBER_MILISECONDS_IN_DAY * lDays;
285 }
286
287
288
289
290
291
292
293 public static DateFormat getDateFormat( Locale locale )
294 {
295 DateFormat dateFormat = null;
296
297 if ( locale != null )
298 {
299 String strLocalizedDateFormat = I18nService.getDateFormatShortPattern( locale );
300
301 if ( StringUtils.isNotEmpty( strLocalizedDateFormat ) )
302 {
303 dateFormat = new SimpleDateFormat( strLocalizedDateFormat );
304 }
305 else
306 {
307 dateFormat = DateFormat.getDateInstance( DateFormat.SHORT, locale );
308 }
309 }
310 else
311 {
312 dateFormat = DateFormat.getDateInstance( DateFormat.SHORT, LocaleService.getDefault( ) );
313 }
314
315 dateFormat.setLenient( false );
316
317 return dateFormat;
318 }
319
320
321
322
323
324
325
326
327 public static Date parseIsoDate( String strDate )
328 {
329 Date date = null;
330 if ( StringUtils.isNotBlank( strDate ) )
331 {
332 SimpleDateFormat format = new SimpleDateFormat( ISO_PATTERN_DATE );
333 try
334 {
335 date = format.parse( strDate );
336 }
337 catch( ParseException e )
338 {
339 return null;
340 }
341 }
342 return date;
343 }
344
345
346
347
348
349
350
351
352 public static String getIsoDateString( Date date )
353 {
354 String strDate = null;
355 if ( date != null )
356 {
357 SimpleDateFormat format = new SimpleDateFormat( ISO_PATTERN_DATE );
358 strDate = format.format( date );
359 }
360 return strDate;
361 }
362 }