|
16 | 16 | public class ISO8601 {
|
17 | 17 |
|
18 | 18 | public static final String PATTERN = "yyyy-MM-dd'T'HH:mm:ssZ";
|
| 19 | + public static final String SPACEY_PATTERN = "yyyy-MM-dd HH:mm:ss Z"; |
19 | 20 | public static final String PATTERN_MSEC = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
|
20 | 21 | public static final String OUTPUT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss'Z'";
|
21 | 22 | public static final String OUTPUT_MSEC_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
|
22 | 23 | public static final String UTC_PATTERN = "yyyy-MM-dd HH:mm:ss 'UTC'";
|
23 | 24 |
|
| 25 | + private static final String PATTERN_REGEX = "\\d\\d\\d\\d-\\d\\d-\\d\\dT\\d\\d:\\d\\d:\\d\\d[-+]\\d\\d\\d\\d"; |
| 26 | + private static final String SPACEY_PATTERN_REGEX = "\\d\\d\\d\\d-\\d\\d-\\d\\d \\d\\d:\\d\\d:\\d\\d [-+]\\d\\d\\d\\d"; |
| 27 | + |
24 | 28 | // Set up ThreadLocal storage to save a thread local SimpleDateFormat keyed with the format string
|
25 | 29 | private static final class SafeDateFormatter {
|
26 | 30 |
|
@@ -128,8 +132,20 @@ public static Date toDate(String dateTimeString) throws ParseException {
|
128 | 132 | if (dateTimeString.endsWith("UTC")) {
|
129 | 133 | return (SafeDateFormatter.getDateFormat(UTC_PATTERN).parse(dateTimeString));
|
130 | 134 | } else {
|
131 |
| - Calendar cal = DatatypeConverter.parseDateTime(dateTimeString); |
132 |
| - return (cal.getTime()); |
| 135 | + try { |
| 136 | + Calendar cal = DatatypeConverter.parseDateTime(dateTimeString); |
| 137 | + return (cal.getTime()); |
| 138 | + } catch (Exception e) { |
| 139 | + if (dateTimeString.matches(PATTERN_REGEX)) { |
| 140 | + // Try using the ISO8601 format |
| 141 | + return (SafeDateFormatter.getDateFormat(PATTERN).parse(dateTimeString)); |
| 142 | + } else if (dateTimeString.matches(SPACEY_PATTERN_REGEX)) { |
| 143 | + // Try using the invalid ISO8601 format with spaces, GitLab sometimes uses this |
| 144 | + return (SafeDateFormatter.getDateFormat(SPACEY_PATTERN).parse(dateTimeString)); |
| 145 | + } else { |
| 146 | + throw e; |
| 147 | + } |
| 148 | + } |
133 | 149 | }
|
134 | 150 | }
|
135 | 151 |
|
|
0 commit comments