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