DateParser.java
5.03 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.commons.util;
import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class DateParser {
private static final Logger log = LoggerFactory.getLogger(DateParser.class);
private static DateFormat[] RFC822_DATE_FORMATS;
private static DateFormat[] W3C_DATE_FORMATS;
private static Pattern w3cTZPattern;
public static Date parseDate(String sDate) {
Date result = null;
result = DateParser.parseRFC822Date(sDate);
if (result == null) {
result = DateParser.parseW3CDate(sDate);
}
return result;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static Date parseRFC822Date(String sDate) {
Date result = null;
if (sDate != null && sDate.length() > 0) {
DateFormat[] arrdateFormat = RFC822_DATE_FORMATS;
synchronized (arrdateFormat) {
result = DateParser.parseDate(sDate, RFC822_DATE_FORMATS, TimeZone.getDefault());
}
}
return result;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static Date parseW3CDate(String sDate) {
Date result = null;
if (sDate != null && sDate.length() > 0) {
TimeZone timeZone = TimeZone.getDefault();
int tzIndex = sDate.lastIndexOf(90);
if (tzIndex != -1 || (tzIndex = sDate.lastIndexOf(45)) != -1 || (tzIndex = sDate.lastIndexOf(43)) != -1) {
timeZone = DateParser.parseW3CTimeZone(sDate.substring(tzIndex));
}
DateFormat[] arrdateFormat = W3C_DATE_FORMATS;
synchronized (arrdateFormat) {
result = DateParser.parseDate(sDate, W3C_DATE_FORMATS, timeZone);
}
}
return result;
}
private static Date parseDate(String sDate, DateFormat[] formats, TimeZone timeZone) {
Date result = null;
for (int i = 0; i < formats.length; ++i) {
formats[i].setTimeZone(timeZone);
try {
formats[i].setLenient(false);
result = formats[i].parse(sDate, new ParsePosition(0));
}
catch (NumberFormatException ex) {
result = null;
}
catch (Exception ex) {
log.error("Unexpected exception while parsing date \"" + sDate + "\" using format \"" + formats[i].toString() + "\"", (Throwable)ex);
}
if (result != null) break;
}
return result;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private static TimeZone parseW3CTimeZone(String tz) {
TimeZone timeZone = null;
Class<DateParser> class_ = DateParser.class;
synchronized (DateParser.class) {
block10 : {
try {
w3cTZPattern = Pattern.compile("^[+-][0-9][0-9]:[0-9][0-9]$");
}
catch (PatternSyntaxException ex) {
if ($assertionsDisabled) break block10;
throw new AssertionError();
}
}
// ** MonitorExit[var2_2] (shouldn't be in output)
switch (tz.charAt(0)) {
case 'Z': {
timeZone = TimeZone.getTimeZone("GMT");
break;
}
case '+':
case '-': {
Matcher matcher = w3cTZPattern.matcher(tz);
if (matcher.matches()) {
timeZone = TimeZone.getTimeZone("GMT" + tz);
}
if (timeZone != null) break;
timeZone = TimeZone.getDefault();
break;
}
default: {
timeZone = TimeZone.getDefault();
}
}
return timeZone;
}
}
static {
W3C_DATE_FORMATS = new SimpleDateFormat[]{new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z"), new SimpleDateFormat("yyyy:MM:dd HH:mm:ss"), new SimpleDateFormat("yyyy:MM:dd HH:mm"), new SimpleDateFormat("yyyy:MM:dd"), new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"), new SimpleDateFormat("yyyy-MM-dd'T'HH:mm"), new SimpleDateFormat("yyyy-MM-dd"), new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"), new SimpleDateFormat("dd.MM.yyyy")};
RFC822_DATE_FORMATS = new SimpleDateFormat[]{new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.US), new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss", Locale.US), new SimpleDateFormat("EEE d MMM yyyy HH:mm:ss", Locale.US)};
w3cTZPattern = null;
}
}