|
29 | 29 | import java.util.TimeZone;
|
30 | 30 |
|
31 | 31 | /**
|
32 |
| - * JsonBuffer implementation borrowed from <a href= |
| 32 | + * DateTimeFormatter implementation borrowed from <a href= |
33 | 33 | * "https://github.com/mongodb/mongo-java-driver/blob/master/bson/src/main/org/bson/json/DateTimeFormatter.java">MongoDB
|
34 | 34 | * Inc.</a> licensed under the Apache License, Version 2.0. <br />
|
35 | 35 | * Formatted and modified.
|
|
40 | 40 | */
|
41 | 41 | class DateTimeFormatter {
|
42 | 42 |
|
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 |
| - |
| 43 | + static long parse(final String dateTimeString) { |
61 | 44 | 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); |
| 45 | + return ISO_OFFSET_DATE_TIME.parse(dateTimeString, new TemporalQuery<Instant>() { |
| 46 | + @Override |
| 47 | + public Instant queryFrom(final TemporalAccessor temporal) { |
| 48 | + return Instant.from(temporal); |
| 49 | + } |
| 50 | + }).toEpochMilli(); |
| 51 | + } catch (DateTimeParseException e) { |
| 52 | + throw new IllegalArgumentException(e.getMessage()); |
76 | 53 | }
|
77 | 54 | }
|
78 | 55 |
|
79 |
| - static long parse(final String dateTimeString) { |
80 |
| - return FORMATTER_IMPL.parse(dateTimeString); |
81 |
| - } |
82 |
| - |
83 | 56 | 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 |
| - } |
| 57 | + return ZonedDateTime.ofInstant(Instant.ofEpochMilli(dateTime), ZoneId.of("Z")).format(ISO_OFFSET_DATE_TIME); |
136 | 58 | }
|
137 | 59 |
|
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 |
| - } |
| 60 | + private DateTimeFormatter() { |
169 | 61 | }
|
170 |
| - |
171 |
| - private DateTimeFormatter() {} |
172 | 62 | }
|
0 commit comments