|
5 | 5 | import java.text.ParseException; |
6 | 6 | import java.text.SimpleDateFormat; |
7 | 7 | import java.util.ArrayList; |
| 8 | +import java.util.Arrays; |
8 | 9 | import java.util.Collections; |
9 | 10 | import java.util.Date; |
10 | 11 | import java.util.HashMap; |
@@ -61,11 +62,18 @@ public interface ReceiptUpdateCallback { |
61 | 62 | void failure(); |
62 | 63 | } |
63 | 64 |
|
64 | | - private final SimpleDateFormat simpleDateFormat; |
| 65 | + private final List<SimpleDateFormat> formats = Arrays.asList( |
| 66 | + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US), |
| 67 | + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US), |
| 68 | + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US) |
| 69 | + ); |
65 | 70 |
|
66 | 71 | public ReceiptsApi() { |
67 | | - simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US); |
68 | | - simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); |
| 72 | + TimeZone utc = TimeZone.getTimeZone("UTC"); |
| 73 | + for (SimpleDateFormat format : formats) { |
| 74 | + format.setTimeZone(utc); |
| 75 | + format.setLenient(false); |
| 76 | + } |
69 | 77 | } |
70 | 78 |
|
71 | 79 | /** |
@@ -147,7 +155,7 @@ public void success(ApiReceipt apiReceipt) { |
147 | 155 | ReceiptInfo receiptInfo = new ReceiptInfo( |
148 | 156 | apiOrder.id, |
149 | 157 | apiOrder.project, |
150 | | - simpleDateFormat.parse(apiOrder.date).getTime(), |
| 158 | + parseDate(apiOrder.date).getTime(), |
151 | 159 | url != null ? snabble.absoluteUrl(url) : null, |
152 | 160 | apiOrder.shopName, |
153 | 161 | priceFormatter.format(apiOrder.price), |
@@ -178,4 +186,17 @@ public void error(Throwable t) { |
178 | 186 | receiptUpdateCallback.failure(); |
179 | 187 | } |
180 | 188 | } |
| 189 | + |
| 190 | + private Date parseDate(String dateStr) throws ParseException { |
| 191 | + ParseException lastException = null; |
| 192 | + for (SimpleDateFormat format : formats) { |
| 193 | + try { |
| 194 | + return format.parse(dateStr); |
| 195 | + } catch (ParseException e) { |
| 196 | + lastException = e; |
| 197 | + } |
| 198 | + } |
| 199 | + assert lastException != null; |
| 200 | + throw lastException; |
| 201 | + } |
181 | 202 | } |
0 commit comments