|
17 | 17 |
|
18 | 18 | import static java.time.format.DateTimeFormatter.*; |
19 | 19 |
|
20 | | -import java.lang.reflect.InvocationTargetException; |
21 | | -import java.lang.reflect.Method; |
22 | 20 | import java.time.Instant; |
| 21 | +import java.time.LocalDate; |
23 | 22 | import java.time.ZoneId; |
| 23 | +import java.time.ZoneOffset; |
24 | 24 | import java.time.ZonedDateTime; |
25 | | -import java.time.format.DateTimeParseException; |
26 | | -import java.time.temporal.TemporalAccessor; |
27 | | -import java.time.temporal.TemporalQuery; |
28 | | -import java.util.Calendar; |
29 | | -import java.util.TimeZone; |
30 | 25 |
|
31 | 26 | /** |
32 | | - * JsonBuffer implementation borrowed from <a href= |
| 27 | + * DateTimeFormatter implementation borrowed from <a href= |
33 | 28 | * "https://github.com/mongodb/mongo-java-driver/blob/master/bson/src/main/org/bson/json/DateTimeFormatter.java">MongoDB |
34 | 29 | * Inc.</a> licensed under the Apache License, Version 2.0. <br /> |
35 | 30 | * Formatted and modified. |
|
40 | 35 | */ |
41 | 36 | class DateTimeFormatter { |
42 | 37 |
|
43 | | - private static final FormatterImpl FORMATTER_IMPL; |
44 | | - |
45 | | - static { |
46 | | - FormatterImpl dateTimeHelper; |
47 | | - try { |
48 | | - dateTimeHelper = loadDateTimeFormatter( |
49 | | - "org.springframework.data.mongodb.util.json.DateTimeFormatter$Java8DateTimeFormatter"); |
50 | | - } catch (LinkageError e) { |
51 | | - // this is expected if running on a release prior to Java 8: fallback to JAXB. |
52 | | - dateTimeHelper = loadDateTimeFormatter( |
53 | | - "org.springframework.data.mongodb.util.json.DateTimeFormatter$JaxbDateTimeFormatter"); |
54 | | - } |
55 | | - |
56 | | - FORMATTER_IMPL = dateTimeHelper; |
57 | | - } |
58 | | - |
59 | | - private static FormatterImpl loadDateTimeFormatter(final String className) { |
60 | | - |
61 | | - try { |
62 | | - return (FormatterImpl) Class.forName(className).getDeclaredConstructor().newInstance(); |
63 | | - } catch (ClassNotFoundException e) { |
64 | | - // this is unexpected as it means the class itself is not found |
65 | | - throw new ExceptionInInitializerError(e); |
66 | | - } catch (InstantiationException e) { |
67 | | - // this is unexpected as it means the class can't be instantiated |
68 | | - throw new ExceptionInInitializerError(e); |
69 | | - } catch (IllegalAccessException e) { |
70 | | - // this is unexpected as it means the no-args constructor isn't accessible |
71 | | - throw new ExceptionInInitializerError(e); |
72 | | - } catch (NoSuchMethodException e) { |
73 | | - throw new ExceptionInInitializerError(e); |
74 | | - } catch (InvocationTargetException e) { |
75 | | - throw new ExceptionInInitializerError(e); |
76 | | - } |
77 | | - } |
| 38 | + private static final int DATE_STRING_LENGTH = "1970-01-01".length(); |
78 | 39 |
|
79 | 40 | static long parse(final String dateTimeString) { |
80 | | - return FORMATTER_IMPL.parse(dateTimeString); |
| 41 | + // ISO_OFFSET_DATE_TIME will not parse date strings consisting of just year-month-day, so use ISO_LOCAL_DATE for |
| 42 | + // those |
| 43 | + if (dateTimeString.length() == DATE_STRING_LENGTH) { |
| 44 | + return LocalDate.parse(dateTimeString, ISO_LOCAL_DATE).atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli(); |
| 45 | + } else { |
| 46 | + return ISO_OFFSET_DATE_TIME.parse(dateTimeString, Instant::from).toEpochMilli(); |
| 47 | + } |
81 | 48 | } |
82 | 49 |
|
83 | 50 | static String format(final long dateTime) { |
84 | | - return FORMATTER_IMPL.format(dateTime); |
85 | | - } |
86 | | - |
87 | | - private interface FormatterImpl { |
88 | | - long parse(String dateTimeString); |
89 | | - |
90 | | - String format(long dateTime); |
91 | | - } |
92 | | - |
93 | | - // Reflective use of DatatypeConverter avoids a compile-time dependency on the java.xml.bind module in Java 9 |
94 | | - static class JaxbDateTimeFormatter implements FormatterImpl { |
95 | | - |
96 | | - private static final Method DATATYPE_CONVERTER_PARSE_DATE_TIME_METHOD; |
97 | | - private static final Method DATATYPE_CONVERTER_PRINT_DATE_TIME_METHOD; |
98 | | - |
99 | | - static { |
100 | | - try { |
101 | | - DATATYPE_CONVERTER_PARSE_DATE_TIME_METHOD = Class.forName("jakarta.xml.bind.DatatypeConverter") |
102 | | - .getDeclaredMethod("parseDateTime", String.class); |
103 | | - DATATYPE_CONVERTER_PRINT_DATE_TIME_METHOD = Class.forName("jakarta.xml.bind.DatatypeConverter") |
104 | | - .getDeclaredMethod("printDateTime", Calendar.class); |
105 | | - } catch (NoSuchMethodException e) { |
106 | | - throw new ExceptionInInitializerError(e); |
107 | | - } catch (ClassNotFoundException e) { |
108 | | - throw new ExceptionInInitializerError(e); |
109 | | - } |
110 | | - } |
111 | | - |
112 | | - @Override |
113 | | - public long parse(final String dateTimeString) { |
114 | | - try { |
115 | | - return ((Calendar) DATATYPE_CONVERTER_PARSE_DATE_TIME_METHOD.invoke(null, dateTimeString)).getTimeInMillis(); |
116 | | - } catch (IllegalAccessException e) { |
117 | | - throw new IllegalStateException(e); |
118 | | - } catch (InvocationTargetException e) { |
119 | | - throw (RuntimeException) e.getCause(); |
120 | | - } |
121 | | - } |
122 | | - |
123 | | - @Override |
124 | | - public String format(final long dateTime) { |
125 | | - Calendar calendar = Calendar.getInstance(); |
126 | | - calendar.setTimeInMillis(dateTime); |
127 | | - calendar.setTimeZone(TimeZone.getTimeZone("Z")); |
128 | | - try { |
129 | | - return (String) DATATYPE_CONVERTER_PRINT_DATE_TIME_METHOD.invoke(null, calendar); |
130 | | - } catch (IllegalAccessException e) { |
131 | | - throw new IllegalStateException(); |
132 | | - } catch (InvocationTargetException e) { |
133 | | - throw (RuntimeException) e.getCause(); |
134 | | - } |
135 | | - } |
| 51 | + return ZonedDateTime.ofInstant(Instant.ofEpochMilli(dateTime), ZoneId.of("Z")).format(ISO_OFFSET_DATE_TIME); |
136 | 52 | } |
137 | 53 |
|
138 | | - static class Java8DateTimeFormatter implements FormatterImpl { |
139 | | - |
140 | | - // if running on Java 8 or above then java.time.format.DateTimeFormatter will be available and initialization will |
141 | | - // succeed. |
142 | | - // Otherwise it will fail. |
143 | | - static { |
144 | | - try { |
145 | | - Class.forName("java.time.format.DateTimeFormatter"); |
146 | | - } catch (ClassNotFoundException e) { |
147 | | - throw new ExceptionInInitializerError(e); |
148 | | - } |
149 | | - } |
150 | | - |
151 | | - @Override |
152 | | - public long parse(final String dateTimeString) { |
153 | | - try { |
154 | | - return ISO_OFFSET_DATE_TIME.parse(dateTimeString, new TemporalQuery<Instant>() { |
155 | | - @Override |
156 | | - public Instant queryFrom(final TemporalAccessor temporal) { |
157 | | - return Instant.from(temporal); |
158 | | - } |
159 | | - }).toEpochMilli(); |
160 | | - } catch (DateTimeParseException e) { |
161 | | - throw new IllegalArgumentException(e.getMessage()); |
162 | | - } |
163 | | - } |
164 | | - |
165 | | - @Override |
166 | | - public String format(final long dateTime) { |
167 | | - return ZonedDateTime.ofInstant(Instant.ofEpochMilli(dateTime), ZoneId.of("Z")).format(ISO_OFFSET_DATE_TIME); |
168 | | - } |
| 54 | + private DateTimeFormatter() { |
169 | 55 | } |
170 | | - |
171 | | - private DateTimeFormatter() {} |
172 | 56 | } |
0 commit comments