1 /*
2 * Copyright (c) 2002-2025, City of Paris
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice
10 * and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice
13 * and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *
32 * License 1.0
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 * This class provides date utils.
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 * Creates a new DateUtil object
59 */
60 private DateUtil( )
61 {
62 }
63
64 /**
65 * Get the date from String date The format pattern is specified internaly
66 *
67 * @param strDate
68 * the date to format
69 * @param locale
70 * The Locale
71 * @return The Date or null else
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 * Get the date from String date
95 *
96 * @param strDate
97 * the date to format
98 * @param locale
99 * The Locale
100 * @return The Date or null else
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 * Get the {@link Timestamp} from String date The format pattern is specified internaly
116 *
117 * @param strDate
118 * the date to format
119 * @param locale
120 * The Locale
121 * @return The {@link Timestamp} or null else
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 * Get the {@link java.sql.Date} from String date The format pattern is specified internaly
137 *
138 * @param strDate
139 * the date to format
140 * @param locale
141 * The Locale
142 * @return The {@link java.sql.Date} or null else
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 * Convert the date to String with a standard pattern
158 *
159 * @param date
160 * The date to convert
161 * @param locale
162 * The Locale
163 * @return The formated String
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 * Converts a long value to a String date
192 *
193 * @param lTime
194 * The long value to convert
195 * @param locale
196 * the locale
197 * @return The formatted string
198 */
199 public static String getDateString( long lTime, Locale locale )
200 {
201 return getDateString( new Date( lTime ), locale );
202 }
203
204 /**
205 * Converts a Timestamp value to a String date
206 *
207 * @param date
208 * The date
209 * @param locale
210 * the locale
211 * @return The formatted string
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 * Converts the current Date to a String date
225 *
226 * @param locale
227 * the locale
228 * @return The formatted string
229 */
230 public static String getCurrentDateString( Locale locale )
231 {
232 return getDateString( new Date( ), locale );
233 }
234
235 /**
236 * Return the pattern for date
237 *
238 * @param locale
239 * the Locale
240 * @return The pattern as a String
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 * Get the number of milliseconds in a given number of days
277 *
278 * @param lDays
279 * The number of days
280 * @return The number of milliseconds in the given number of days
281 */
282 public static long convertDaysInMiliseconds( long lDays )
283 {
284 return CONSTANT_NUMBER_MILISECONDS_IN_DAY * lDays;
285 }
286
287 /**
288 * Get the Date format for the locale
289 *
290 * @param locale
291 * @return
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 * Parse a date from ISO format ( yyyy-MM-dd HH:mm:ss )
322 *
323 * @param strDate
324 * @since 7.0.1
325 * @return parsed {@link Date} or null if the string can't be parsed
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 * Get the ISO format ( yyyy-MM-dd HH:mm:ss ) of a date
347 *
348 * @param date
349 * @since 7.0.1
350 * @return string of the date in iso format, or null if param is null
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 }