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
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
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
55
56 public static final String DATE_YYYYMMDD_AVEC_TIRET = "yyyy-MM-dd";
57
58 private DateFormatUtils( )
59 {
60
61 }
62
63
64
65
66
67
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
81
82
83
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
97
98
99
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
113
114
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
129
130
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
152
153
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
173
174
175
176
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
191
192
193
194
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
214
215
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
228
229
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 }