View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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  
35  package fr.paris.lutece.plugins.utilitairesfo.utils;
36  
37  import java.sql.Timestamp;
38  import java.text.DateFormat;
39  import java.text.ParseException;
40  import java.text.SimpleDateFormat;
41  import java.util.Calendar;
42  import java.util.Date;
43  import java.util.Locale;
44  
45  
46  public class DateFormatUtils
47  {
48      /**
49       * Format de date jour/mois/année (RSF)
50       */
51      public static final String DATE_DDMMYYYY_AVEC_SLASH = "dd/MM/yyyy";
52      public static final String DATE_DDMMMMYYYY_AVEC_ESPACES = "dd MMMM yyyy";
53      /**
54       * Format de date année-mois-jour (GIPSE)
55       */
56      public static final String DATE_YYYYMMDD_AVEC_TIRET = "yyyy-MM-dd";
57  
58      private DateFormatUtils( )
59      {
60  
61      }
62  
63      /**
64       * retourne une {@link Date} depuis une chaine au format {@link #DATE_DDMMYYYY_AVEC_SLASH jour/mois/année}
65       * @param strDate String date
66       * @return Date
67       * @throws ParseException erreur de parsing de la date
68       */
69      public static Date formattageStringToUtilDateDDMMYYYY_avecSlash( String strDate ) throws ParseException
70      {
71          if ( strDate == null || strDate.isEmpty( ) )
72          {
73              return null;
74          }
75  
76          return DateFormatUtils.parseDateAvecPattern( strDate, DATE_DDMMYYYY_AVEC_SLASH );
77      }
78  
79      /**
80       * retourne une {@link Date} depuis une chaine au format {@link #DATE_DDMMMMYYYY_AVEC_ESPACES jour/mois/année}
81       * @param strDate String date
82       * @return Date
83       * @throws ParseException erreur de parsing de la date
84       */
85      public static Date formattageStringToUtilDateDDMMMMYYYY_avecSlash( String strDate ) throws ParseException
86      {
87          if ( strDate == null || strDate.isEmpty( ) )
88          {
89              return null;
90          }
91  
92          return DateFormatUtils.parseDateAvecPattern( strDate, DATE_DDMMMMYYYY_AVEC_ESPACES, Locale.FRANCE );
93      }
94  
95      /**
96       * retourne une {@link Date} depuis une chaine au format {@link #DATE_DDMMMMYYYY_AVEC_ESPACES jour/mois/année}
97       * @param strDate String date
98       * @return Date
99       * @throws ParseException erreur de parsing de la date
100      */
101     public static Date formattageStringToUtilDateDDMMMMYYYY_avecSlash( String strDate, Locale locale ) throws ParseException
102     {
103         if ( strDate == null || strDate.isEmpty( ) )
104         {
105             return null;
106         }
107 
108         return DateFormatUtils.parseDateAvecPattern( strDate, DATE_DDMMMMYYYY_AVEC_ESPACES, locale );
109     }
110 
111     /**
112      * retourne une {@link java.sql.Date Date} depuis une chaine au format {@link #DATE_DDMMYYYY_AVEC_SLASH jour/mois/année}
113      * @param strDate
114      * @return
115      */
116     public static java.sql.Date formattageStringToSqlDateDDMMYYYY_avecSlash( String strDate ) throws ParseException
117     {
118 
119         if ( strDate == null || strDate.isEmpty( ) )
120         {
121             return null;
122         }
123 
124         return new java.sql.Date( new SimpleDateFormat( DATE_DDMMYYYY_AVEC_SLASH ).parse( strDate ).getTime( ) );
125     }
126 
127     /**
128      * retourne une {@link java.sql.Date Date} depuis une chaine au format {@link #DATE_DDMMYYYY_AVEC_SLASH jour/mois/année}
129      * @param strDate
130      * @return
131      */
132     public static java.sql.Date formattageStringToSqlDateDDMMYYYY_avecSlashSansException( String strDate )
133     {
134 
135         if ( strDate == null || strDate.isEmpty( ) )
136         {
137             return null;
138         }
139 
140         try
141         {
142             return new java.sql.Date( new SimpleDateFormat( DATE_DDMMYYYY_AVEC_SLASH ).parse( strDate ).getTime( ) );
143         }
144         catch ( ParseException e )
145         {
146             return null;
147         }
148     }
149 
150     /**
151      * format {@link #DATE_DDMMYYYY_AVEC_SLASH jour/mois/année}
152      * @param date
153      * @return
154      */
155     public static String formattageDateToStringDDMMYYYY_avecSlash( Date date )
156     {
157         return formattageDateToStringAvecPattern( date, DATE_DDMMYYYY_AVEC_SLASH, null );
158     }
159 
160     public static String formattageDateToStringDDMMMMYYYY_avecEspace( Date date, Locale locale )
161     {
162         return formattageDateToStringAvecPattern( date, DATE_DDMMMMYYYY_AVEC_ESPACES, locale );
163     }
164 
165     public static String formattageDateToStringDDMMMMYYYY_avecEspace( Date date )
166     {
167         return formattageDateToStringAvecPattern( date, DATE_DDMMMMYYYY_AVEC_ESPACES, Locale.FRANCE );
168     }
169 
170 
171     /**
172      * Parse la date avec le format fourni
173      * @param strDate String date
174      * @param formatDate format de date
175      * @return Date
176      * @throws ParseException erreur de parsing de la date
177      */
178     public static Date parseDateAvecPattern( String strDate, String formatDate ) throws ParseException
179     {
180         if ( StringUtils.isBlank( strDate ) || StringUtils.isBlank( formatDate ) )
181         {
182             return null;
183         }
184 
185         SimpleDateFormat formatter = new SimpleDateFormat( formatDate );
186         formatter.setLenient( false );
187         return formatter.parse( strDate );
188     }
189     /**
190      * Parse la date avec le format fourni
191      * @param strDate String date
192      * @param formatDate format de date
193      * @return Date
194      * @throws ParseException erreur de parsing de la date
195      */
196     public static Date parseDateAvecPattern( String strDate, String formatDate, Locale locale ) throws ParseException
197     {
198         if ( locale == null )
199         {
200             return parseDateAvecPattern( strDate, formatDate );
201         }
202 
203         if ( StringUtils.isBlank( strDate ) || StringUtils.isBlank( formatDate ) )
204         {
205             return null;
206         }
207         SimpleDateFormat formatter = new SimpleDateFormat( formatDate, locale );
208         formatter.setLenient( false );
209         return formatter.parse( strDate );
210     }
211 
212     /**
213      * Transforme une {@link java.util.Date} en {@link java.sql.Date}
214      * @param date
215      * @return
216      */
217     public static java.sql.Date toSqlDate( java.util.Date date )
218     {
219         if ( date == null )
220         {
221             return null;
222         }
223         return new java.sql.Date( date.getTime( ) );
224     }
225 
226     /**
227      * Transforme une {@link java.sql.Date} en {@link java.util.Date}
228      * @param date
229      * @return
230      */
231     public static java.util.Date toUtilDate( java.sql.Date date )
232     {
233         if ( date == null )
234         {
235             return null;
236         }
237         return new java.util.Date( date.getTime( ) );
238     }
239 
240     public static String formattageddMMYYYYversddMMMMYYYY( String date) throws ParseException
241     {
242         Date date1 = formattageStringToUtilDateDDMMYYYY_avecSlash( date );
243         return formattageDateToStringDDMMMMYYYY_avecEspace( date1 );
244     }
245 
246     public static String formattageddMMMMYYYYversddMMYYYY( String date) throws ParseException
247     {
248         Date date1 = formattageStringToUtilDateDDMMYYYY_avecSlash( date );
249         return formattageDateToStringDDMMMMYYYY_avecEspace( date1 );
250     }
251 
252     public static Timestamp toTimestamp( Date date, boolean tronquerMillisecondes )
253     {
254         if ( date == null )
255         {
256             return null;
257         }
258 
259         long time;
260         if ( tronquerMillisecondes )
261         {
262             Calendar cal = Calendar.getInstance( );
263             cal.setTime( date );
264             cal.set(Calendar.MILLISECOND, 0);
265             time = cal.getTimeInMillis();
266         }
267         else
268         {
269             time = date.getTime( );
270         }
271 
272 
273         return new Timestamp( time );
274     }
275 
276     public static Timestamp toTimestamp( Date date )
277     {
278         return toTimestamp( date, false );
279     }
280 
281     public static Date toJavaUtilDate( Timestamp ts )
282     {
283         if ( ts == null )
284         {
285             return null;
286         }
287         return new Date( ts.getTime( ) );
288     }
289 
290     public static String formattageDateToStringAvecPattern( Date date, String patternDate, Locale locale )
291     {
292         if ( date == null || patternDate == null )
293         {
294             return null;
295         }
296 
297         DateFormat formatter;
298         if ( locale == null )
299         {
300             formatter = new SimpleDateFormat( patternDate );
301         }
302         else
303         {
304             formatter = new SimpleDateFormat( patternDate, locale );
305         }
306 
307         return formatter.format( date );
308     }
309 
310     public static String formattageStringToStringAvecPattern( String date, String patternDateEntree, String patternDateSortie, Locale locale ) throws ParseException
311     {
312         if ( StringUtils.isBlank( date ) || StringUtils.isBlank( patternDateEntree ) || StringUtils.isBlank( patternDateSortie ) )
313         {
314             return null;
315         }
316 
317         Date date1 = parseDateAvecPattern( date, patternDateEntree, locale );
318         return formattageDateToStringAvecPattern( date1, patternDateSortie, locale );
319     }
320 }