|
19 | 19 | import java.math.BigInteger;
|
20 | 20 | import java.text.DateFormat;
|
21 | 21 | import java.text.ParseException;
|
| 22 | +import java.text.SimpleDateFormat; |
22 | 23 | import java.time.Instant;
|
23 | 24 | import java.time.LocalDate;
|
24 | 25 | import java.time.LocalDateTime;
|
@@ -194,6 +195,25 @@ private LocalDate parseStringToLocalDate(String input) {
|
194 | 195 | };
|
195 | 196 |
|
196 | 197 | public static class GraphQLDateCoercing implements Coercing<Object, Object> {
|
| 198 | + final DateFormat df; |
| 199 | + |
| 200 | + |
| 201 | + /** |
| 202 | + * Parse date strings matching DateFormat's locale-sensitive SHORT pattern, |
| 203 | + * see: https://docs.oracle.com/javase/tutorial/i18n/format/dateFormat.html |
| 204 | + */ |
| 205 | + public GraphQLDateCoercing() { |
| 206 | + df = DateFormat.getInstance(); |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * Parse date strings according to the provided SimpleDateFormat pattern |
| 211 | + * |
| 212 | + * @param dateFormatString e.g. "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" for "2001-07-04T12:08:56.235-07:00" |
| 213 | + */ |
| 214 | + public GraphQLDateCoercing(String dateFormatString) { |
| 215 | + df = new SimpleDateFormat(dateFormatString); |
| 216 | + } |
197 | 217 |
|
198 | 218 | @Override
|
199 | 219 | public Object serialize(Object input) {
|
@@ -227,7 +247,7 @@ public Object parseLiteral(Object input) {
|
227 | 247 |
|
228 | 248 | private Date parseStringToDate(String input) {
|
229 | 249 | try {
|
230 |
| - return DateFormat.getInstance().parse(input); |
| 250 | + return df.parse(input); |
231 | 251 | } catch (ParseException e) {
|
232 | 252 | log.warn("Failed to parse Date from input: " + input, e);
|
233 | 253 | return null;
|
|
0 commit comments