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.plugins.formengine.util;
35  
36  import fr.paris.lutece.portal.service.util.AppLogService;
37  
38  import java.sql.Time;
39  import java.sql.Timestamp;
40  
41  import java.text.DateFormat;
42  import java.text.ParseException;
43  import java.text.SimpleDateFormat;
44  
45  import java.util.Calendar;
46  import java.util.Date;
47  import java.util.GregorianCalendar;
48  import java.util.Locale;
49  
50  
51  /**
52   * NoticeUtils
53   */
54  public final class NoticeUtils
55  {
56      public static final String EMPTY_STRING = "";
57      public static final String CONSTANTE_PATTERN_DATE = "dd/MM/yyyy";
58      private static final String REGEX_ID = "^[\\d]+$";
59  
60      /** Private constructor */
61      private NoticeUtils(  )
62      {
63      }
64  
65      /**
66      * convert a string to int
67      *
68      * @param strParameter
69      *            the string parameter to convert
70      * @return the conversion
71      */
72      public static int convertStringToInt( String strParameter )
73      {
74          int nIdParameter = -1;
75  
76          try
77          {
78              if ( ( strParameter != null ) && strParameter.matches( REGEX_ID ) )
79              {
80                  nIdParameter = Integer.parseInt( strParameter );
81              }
82          }
83          catch ( NumberFormatException ne )
84          {
85              AppLogService.error( ne );
86          }
87  
88          return nIdParameter;
89      }
90  
91      /**
92       * Renvoie la date sous le format d�fini par strPattern
93       * @param date la date
94       * @param strPattern le format souhaite de la date
95       * @return la date sous forme EEEE dd MMMM yyyy
96       */
97      public static String getDate( Timestamp date, String strPattern )
98      {
99          String strUsedPattern = strPattern;
100 
101         if ( strUsedPattern == null )
102         {
103             strUsedPattern = CONSTANTE_PATTERN_DATE;
104         }
105 
106         SimpleDateFormat dateFormat = new SimpleDateFormat( strUsedPattern, Locale.FRENCH );
107         String strDate = dateFormat.format( date ).toString(  );
108 
109         return strDate;
110     }
111 
112     /**
113      * Transfome une date en format string de type dd/MM/yyyy en objet Timestamp
114      * @param strDate Date � transformer
115      * @param isStartOfDayHour TRUE si l'heure doit etre 00h01 FALSE si l'heure doit etre 23H59
116      * @return objet Timestamp correspondant � la date donn� en param�tre
117      */
118     public static Timestamp getDate( String strDate, boolean isStartOfDayHour )
119     {
120         if ( ( strDate == null ) || ( strDate.trim(  ).length(  ) != CONSTANTE_PATTERN_DATE.length(  ) ) )
121         {
122             return null;
123         }
124 
125         DateFormat dateFormat = new SimpleDateFormat( CONSTANTE_PATTERN_DATE, Locale.FRENCH );
126         dateFormat.setLenient( false );
127 
128         Date date;
129 
130         try
131         {
132             date = dateFormat.parse( strDate.trim(  ) );
133         }
134         catch ( ParseException e )
135         {
136             return null;
137         }
138 
139         Calendar caldate = new GregorianCalendar(  );
140         caldate.setTime( date );
141         caldate.set( Calendar.MILLISECOND, 0 );
142         caldate.set( Calendar.SECOND, 0 );
143 
144         if ( isStartOfDayHour )
145         {
146             caldate.set( Calendar.HOUR_OF_DAY, caldate.getActualMinimum( Calendar.HOUR_OF_DAY ) );
147             caldate.set( Calendar.MINUTE, caldate.getActualMinimum( Calendar.MINUTE ) );
148         }
149         else
150         {
151             caldate.set( Calendar.HOUR_OF_DAY, caldate.getActualMaximum( Calendar.HOUR_OF_DAY ) );
152             caldate.set( Calendar.MINUTE, caldate.getActualMaximum( Calendar.MINUTE ) );
153             caldate.set( Calendar.SECOND, caldate.getActualMaximum( Calendar.SECOND ) );
154         }
155 
156         Timestamp timeStamp = new Timestamp( caldate.getTimeInMillis(  ) );
157 
158         return timeStamp;
159     }
160 
161     /**
162      * Retourne la date du jour
163      * @return la date du jour
164      */
165     public static Timestamp getCurrentDate(  )
166     {
167         return new Timestamp( GregorianCalendar.getInstance(  ).getTimeInMillis(  ) );
168     }
169 
170     /**
171      * Retourne l'heure de la Date
172      * @param date Date
173      * @return Time l'heure de la date
174      */
175     public static Time getTime( Date date )
176     {
177         Calendar cal = Calendar.getInstance(  );
178         cal.setTime( date );
179 
180         Time time = new Time( cal.getTimeInMillis(  ) );
181 
182         return time;
183     }
184 }