View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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 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   * This class provides date utils.
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       * Creates a new DateUtil object
65       */
66      private DateUtil(  )
67      {
68      }
69  
70      /**
71       * Returns the date of the day in form of a String
72       *
73       * @return The Date of the day in a  "JJ/MM/AAAA" format
74       * @deprecated Use getCurrentDateString( Locale locale )
75       */
76      public static synchronized String getCurrentDateString(  )
77      {
78          return _formatter.format( new java.util.Date(  ) );
79      }
80  
81      /**
82       * Converts a String date in a "jj/mm/aaaa" format in a java.sql.Date type date
83       *
84       * @param strDate The String Date to convert, in a date in the "jj/mm/aaaa" format
85       * @return The date in form of a java.sql.Date type date
86       * @deprecated Use formatDateSql( String strDate, Locale locale )
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      * Converts a String date in a "jj/mm/aaaa" format in a java.util.Date type date
103      *
104      * @param strDate The String Date to convert, in a date in the "jj/mm/aaaa" format
105      * @return The date in form of a java.sql.Date tyep date
106      * @deprecated Use formatDate( String strDate, Locale locale )
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      * Converts a String date in a "jj/mm/aaaa" format in a java.sql.Timestamp type date
118      *
119      * @param strDate The String Date to convert, in a date in the "jj/mm/aaaa" format
120      * @return The date in form of a java.sql.Date tyep date
121      * @deprecated Use formatTimestamp( String strDate, Locale locale )
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      * Converts a java.sql.Date type date in a String date with a "jj/mm/aaaa" format
138      *
139      * @param date java.sql.Date date to convert
140      * @return strDate The date converted to String in a "jj/mm/aaaa" format or an empty String if the date is null
141      * @deprecated Use getDateString( Date date, Locale locale )
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     // methodes using the java.sql.Timestamp type
158 
159     /**
160      * Converts une java.sql.Timestamp date in a String date in a "jj/mm/aaaa" format
161      *
162      * @param date java.sql.Timestamp date to convert
163      * @return strDate The String date in a "jj/mm/aaaa" format or the emmpty String if the date is null
164      * @deprecated Use getDateString( java.sql.Timestamp date, Locale locale )
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     // methodes using the java.util.Date type
181 
182     /**
183      * Converts a java.util.Date date in a String date in a "jj/mm/aaaa" format
184      *
185      * @param date java.util.Date date to convert
186      * @return strDate A String date in a "jj/mm/aaaa" format or an empty String if the date is null
187      * @deprecated Use formatDate( String strDate, Locale locale )
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     // methods using a long value
204 
205     //TODO : Create a new getDateTimeString(  ) function with locale
206     /**
207      * Converts a long value to a String date in a "jj/mm/aaaa hh:mm" format
208      *
209      * @param lTime The long value to convert
210      * @return The formatted string
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     /* -------------- Added in 2.2.1 ----------------- */
221 
222     /**
223      * Get the date from String date
224      * The format pattern is specified internaly
225      * @param strDate the date to format
226      * @param locale The Locale
227      * @return The Date or null else
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      * Get the date from String date
272      * @param strDate the date to format
273      * @param locale The Locale
274      * @return The Date or null else
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      * Get the {@link Timestamp} from String date
290      * The format pattern is specified internaly
291      * @param strDate the date to format
292      * @param locale The Locale
293      * @return The {@link Timestamp} or null else
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      * Get the {@link java.sql.Date} from String date
309      * The format pattern is specified internaly
310      * @param strDate the date to format
311      * @param locale The Locale
312      * @return The {@link java.sql.Date} or null else
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      * Convert the date to String with a standard pattern
328      * @param date The date to convert
329      * @param locale The Locale
330      * @return The formated String
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      * Converts a long value to a String date
359      *
360      * @param lTime The long value to convert
361      * @param locale the locale
362      * @return The formatted string
363      */
364     public static String getDateString( long lTime, Locale locale )
365     {
366         return getDateString( new Date( lTime ), locale );
367     }
368 
369     /**
370      * Converts a Timestamp value to a String date
371      *
372      * @param date The date
373      * @param locale the locale
374      * @return The formatted string
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      * Converts the current Date to a String date
388      *
389      * @param locale the locale
390      * @return The formatted string
391      */
392     public static String getCurrentDateString( Locale locale )
393     {
394         return getDateString( new Date(  ), locale );
395     }
396 
397     /**
398      * Return the pattern for date
399      * @param locale the Locale
400      * @return The pattern as a String
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      * Get the number of milliseconds in a given number of days
437      * @param lDays The number of days
438      * @return The number of milliseconds in the given number of days
439      */
440     public static long convertDaysInMiliseconds( long lDays )
441     {
442         return CONSTANT_NUMBER_MILISECONDS_IN_DAY * lDays;
443     }
444 }