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 package org.apache.hc.client5.http.utils;
29
30 import java.time.Instant;
31 import java.time.LocalDateTime;
32 import java.time.ZoneId;
33 import java.time.ZoneOffset;
34 import java.time.format.DateTimeFormatter;
35 import java.time.format.DateTimeFormatterBuilder;
36 import java.time.format.DateTimeParseException;
37 import java.util.Date;
38 import java.util.Locale;
39 import java.util.TimeZone;
40
41 import org.apache.hc.core5.http.Header;
42 import org.apache.hc.core5.http.MessageHeaders;
43 import org.apache.hc.core5.util.Args;
44
45
46
47
48
49
50
51 public final class DateUtils {
52
53
54
55
56 @Deprecated
57 public static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";
58 public static final String INTERNET_MESSAGE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss zzz";
59
60
61
62
63
64
65
66 public static final DateTimeFormatter FORMATTER_RFC1123 = new DateTimeFormatterBuilder()
67 .parseLenient()
68 .parseCaseInsensitive()
69 .appendPattern(INTERNET_MESSAGE_FORMAT)
70 .toFormatter(Locale.ENGLISH);
71
72
73
74
75 public static final String PATTERN_RFC1036 = "EEE, dd-MMM-yy HH:mm:ss zzz";
76
77
78
79
80
81
82 public static final DateTimeFormatter FORMATTER_RFC1036 = new DateTimeFormatterBuilder()
83 .parseLenient()
84 .parseCaseInsensitive()
85 .appendPattern(PATTERN_RFC1036)
86 .toFormatter(Locale.ENGLISH);
87
88
89
90
91
92 public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy";
93
94
95
96
97
98
99 public static final DateTimeFormatter FORMATTER_ASCTIME = new DateTimeFormatterBuilder()
100 .parseLenient()
101 .parseCaseInsensitive()
102 .appendPattern(PATTERN_ASCTIME)
103 .toFormatter(Locale.ENGLISH);
104
105
106
107
108
109
110 public static final DateTimeFormatter[] STANDARD_PATTERNS = new DateTimeFormatter[] {
111 FORMATTER_RFC1123,
112 FORMATTER_RFC1036,
113 FORMATTER_ASCTIME
114 };
115
116 static final ZoneId GMT_ID = ZoneId.of("GMT");
117
118
119
120
121 public static Date toDate(final Instant instant) {
122 return instant != null ? new Date(instant.toEpochMilli()) : null;
123 }
124
125
126
127
128 public static Instant toInstant(final Date date) {
129 return date != null ? Instant.ofEpochMilli(date.getTime()) : null;
130 }
131
132
133
134
135 public static LocalDateTime toUTC(final Instant instant) {
136 return instant != null ? instant.atZone(ZoneOffset.UTC).toLocalDateTime() : null;
137 }
138
139
140
141
142 public static LocalDateTime toUTC(final Date date) {
143 return toUTC(toInstant(date));
144 }
145
146
147
148
149
150
151
152
153
154
155
156 public static Instant parseDate(final String dateValue, final DateTimeFormatter... dateFormatters) {
157 Args.notNull(dateValue, "Date value");
158 String v = dateValue;
159
160
161 if (v.length() > 1 && v.startsWith("'") && v.endsWith("'")) {
162 v = v.substring (1, v.length() - 1);
163 }
164
165 for (final DateTimeFormatter dateFormatter : dateFormatters) {
166 try {
167 return Instant.from(dateFormatter.parse(v));
168 } catch (final DateTimeParseException ignore) {
169 }
170 }
171 return null;
172 }
173
174
175
176
177
178
179
180
181
182
183
184 public static Instant parseStandardDate(final String dateValue) {
185 return parseDate(dateValue, STANDARD_PATTERNS);
186 }
187
188
189
190
191
192
193
194
195
196
197
198 public static Instant parseStandardDate(final MessageHeaders headers, final String headerName) {
199 if (headers == null) {
200 return null;
201 }
202 final Header header = headers.getFirstHeader(headerName);
203 if (header == null) {
204 return null;
205 }
206 return parseStandardDate(header.getValue());
207 }
208
209
210
211
212
213
214
215
216
217
218
219 public static String formatStandardDate(final Instant instant) {
220 return formatDate(instant, FORMATTER_RFC1123);
221 }
222
223
224
225
226
227
228
229
230
231
232
233
234 public static String formatDate(final Instant instant, final DateTimeFormatter dateTimeFormatter) {
235 Args.notNull(instant, "Instant");
236 Args.notNull(dateTimeFormatter, "DateTimeFormatter");
237 return dateTimeFormatter.format(instant.atZone(GMT_ID));
238 }
239
240
241
242
243 @Deprecated
244 public static final TimeZone GMT = TimeZone.getTimeZone("GMT");
245
246
247
248
249
250
251
252
253
254
255
256 @Deprecated
257 public static Date parseDate(final String dateValue) {
258 return parseDate(dateValue, null, null);
259 }
260
261
262
263
264
265
266
267
268
269
270
271
272
273 @Deprecated
274 public static Date parseDate(final MessageHeaders headers, final String headerName) {
275 return toDate(parseStandardDate(headers, headerName));
276 }
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294 @Deprecated
295 public static boolean isAfter(
296 final MessageHeaders message1,
297 final MessageHeaders message2,
298 final String headerName) {
299 if (message1 != null && message2 != null) {
300 final Header dateHeader1 = message1.getFirstHeader(headerName);
301 if (dateHeader1 != null) {
302 final Header dateHeader2 = message2.getFirstHeader(headerName);
303 if (dateHeader2 != null) {
304 final Date date1 = parseDate(dateHeader1.getValue());
305 if (date1 != null) {
306 final Date date2 = parseDate(dateHeader2.getValue());
307 if (date2 != null) {
308 return date1.after(date2);
309 }
310 }
311 }
312 }
313 }
314 return false;
315 }
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333 @Deprecated
334 public static boolean isBefore(
335 final MessageHeaders message1,
336 final MessageHeaders message2,
337 final String headerName) {
338 if (message1 != null && message2 != null) {
339 final Header dateHeader1 = message1.getFirstHeader(headerName);
340 if (dateHeader1 != null) {
341 final Header dateHeader2 = message2.getFirstHeader(headerName);
342 if (dateHeader2 != null) {
343 final Date date1 = parseDate(dateHeader1.getValue());
344 if (date1 != null) {
345 final Date date2 = parseDate(dateHeader2.getValue());
346 if (date2 != null) {
347 return date1.before(date2);
348 }
349 }
350 }
351 }
352 }
353 return false;
354 }
355
356
357
358
359
360
361
362
363
364
365
366 @Deprecated
367 public static Date parseDate(final String dateValue, final String[] dateFormats) {
368 return parseDate(dateValue, dateFormats, null);
369 }
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385 @Deprecated
386 public static Date parseDate(
387 final String dateValue,
388 final String[] dateFormats,
389 final Date startDate) {
390 final DateTimeFormatter[] dateTimeFormatters;
391 if (dateFormats != null) {
392 dateTimeFormatters = new DateTimeFormatter[dateFormats.length];
393 for (int i = 0; i < dateFormats.length; i++) {
394 dateTimeFormatters[i] = new DateTimeFormatterBuilder()
395 .parseLenient()
396 .parseCaseInsensitive()
397 .appendPattern(dateFormats[i])
398 .toFormatter();
399 }
400 } else {
401 dateTimeFormatters = STANDARD_PATTERNS;
402 }
403 return toDate(parseDate(dateValue, dateTimeFormatters));
404 }
405
406
407
408
409
410
411
412
413
414
415
416 @Deprecated
417 public static String formatDate(final Date date) {
418 return formatStandardDate(toInstant(date));
419 }
420
421
422
423
424
425
426
427
428
429
430
431
432 @Deprecated
433 public static String formatDate(final Date date, final String pattern) {
434 Args.notNull(date, "Date");
435 Args.notNull(pattern, "Pattern");
436 return DateTimeFormatter.ofPattern(pattern).format(toInstant(date).atZone(GMT_ID));
437 }
438
439
440
441
442
443
444
445
446 @Deprecated
447 public static void clearThreadLocal() {
448 }
449
450
451 private DateUtils() {
452 }
453
454 }