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.DateTimeException;
31 import java.time.Instant;
32 import java.time.LocalDateTime;
33 import java.time.ZoneId;
34 import java.time.ZoneOffset;
35 import java.time.format.DateTimeFormatter;
36 import java.time.format.DateTimeFormatterBuilder;
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
157
158 public static Instant parseDate(final String dateValue, final DateTimeFormatter... dateFormatters) {
159 Args.notNull(dateValue, "Date value");
160 String v = dateValue;
161
162
163 if (v.length() > 1 && v.startsWith("'") && v.endsWith("'")) {
164 v = v.substring (1, v.length() - 1);
165 }
166
167 for (final DateTimeFormatter dateFormatter : dateFormatters) {
168 try {
169 return Instant.from(dateFormatter.parse(v));
170 } catch (final DateTimeException ignore) {
171 }
172 }
173 return null;
174 }
175
176
177
178
179
180
181
182
183
184
185
186 public static Instant parseStandardDate(final String dateValue) {
187 return parseDate(dateValue, STANDARD_PATTERNS);
188 }
189
190
191
192
193
194
195
196
197
198
199
200 public static Instant parseStandardDate(final MessageHeaders headers, final String headerName) {
201 if (headers == null) {
202 return null;
203 }
204 final Header header = headers.getFirstHeader(headerName);
205 if (header == null) {
206 return null;
207 }
208 return parseStandardDate(header.getValue());
209 }
210
211
212
213
214
215
216
217
218
219
220
221 public static String formatStandardDate(final Instant instant) {
222 return formatDate(instant, FORMATTER_RFC1123);
223 }
224
225
226
227
228
229
230
231
232
233
234
235
236 public static String formatDate(final Instant instant, final DateTimeFormatter dateTimeFormatter) {
237 Args.notNull(instant, "Instant");
238 Args.notNull(dateTimeFormatter, "DateTimeFormatter");
239 return dateTimeFormatter.format(instant.atZone(GMT_ID));
240 }
241
242
243
244
245 @Deprecated
246 public static final TimeZone GMT = TimeZone.getTimeZone("GMT");
247
248
249
250
251
252
253
254
255
256
257
258 @Deprecated
259 public static Date parseDate(final String dateValue) {
260 return parseDate(dateValue, null, null);
261 }
262
263
264
265
266
267
268
269
270
271
272
273
274
275 @Deprecated
276 public static Date parseDate(final MessageHeaders headers, final String headerName) {
277 return toDate(parseStandardDate(headers, headerName));
278 }
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296 @Deprecated
297 public static boolean isAfter(
298 final MessageHeaders message1,
299 final MessageHeaders message2,
300 final String headerName) {
301 if (message1 != null && message2 != null) {
302 final Header dateHeader1 = message1.getFirstHeader(headerName);
303 if (dateHeader1 != null) {
304 final Header dateHeader2 = message2.getFirstHeader(headerName);
305 if (dateHeader2 != null) {
306 final Date date1 = parseDate(dateHeader1.getValue());
307 if (date1 != null) {
308 final Date date2 = parseDate(dateHeader2.getValue());
309 if (date2 != null) {
310 return date1.after(date2);
311 }
312 }
313 }
314 }
315 }
316 return false;
317 }
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335 @Deprecated
336 public static boolean isBefore(
337 final MessageHeaders message1,
338 final MessageHeaders message2,
339 final String headerName) {
340 if (message1 != null && message2 != null) {
341 final Header dateHeader1 = message1.getFirstHeader(headerName);
342 if (dateHeader1 != null) {
343 final Header dateHeader2 = message2.getFirstHeader(headerName);
344 if (dateHeader2 != null) {
345 final Date date1 = parseDate(dateHeader1.getValue());
346 if (date1 != null) {
347 final Date date2 = parseDate(dateHeader2.getValue());
348 if (date2 != null) {
349 return date1.before(date2);
350 }
351 }
352 }
353 }
354 }
355 return false;
356 }
357
358
359
360
361
362
363
364
365
366
367
368 @Deprecated
369 public static Date parseDate(final String dateValue, final String[] dateFormats) {
370 return parseDate(dateValue, dateFormats, null);
371 }
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387 @Deprecated
388 public static Date parseDate(
389 final String dateValue,
390 final String[] dateFormats,
391 final Date startDate) {
392 final DateTimeFormatter[] dateTimeFormatters;
393 if (dateFormats != null) {
394 dateTimeFormatters = new DateTimeFormatter[dateFormats.length];
395 for (int i = 0; i < dateFormats.length; i++) {
396 dateTimeFormatters[i] = new DateTimeFormatterBuilder()
397 .parseLenient()
398 .parseCaseInsensitive()
399 .appendPattern(dateFormats[i])
400 .toFormatter();
401 }
402 } else {
403 dateTimeFormatters = STANDARD_PATTERNS;
404 }
405 return toDate(parseDate(dateValue, dateTimeFormatters));
406 }
407
408
409
410
411
412
413
414
415
416
417
418 @Deprecated
419 public static String formatDate(final Date date) {
420 return formatStandardDate(toInstant(date));
421 }
422
423
424
425
426
427
428
429
430
431
432
433
434 @Deprecated
435 public static String formatDate(final Date date, final String pattern) {
436 Args.notNull(date, "Date");
437 Args.notNull(pattern, "Pattern");
438 return DateTimeFormatter.ofPattern(pattern).format(toInstant(date).atZone(GMT_ID));
439 }
440
441
442
443
444
445
446
447
448 @Deprecated
449 public static void clearThreadLocal() {
450 }
451
452
453 private DateUtils() {
454 }
455
456 }