29
29
import java .time .ZonedDateTime ;
30
30
import java .time .format .DateTimeFormatter ;
31
31
import java .time .format .FormatStyle ;
32
- import java .util .HashMap ;
32
+ import java .util .EnumMap ;
33
33
import java .util .Map ;
34
34
35
35
import org .springframework .format .FormatterRegistrar ;
@@ -56,18 +56,17 @@ private enum Type {DATE, TIME, DATE_TIME}
56
56
57
57
58
58
/**
59
- * User defined formatters.
59
+ * User- defined formatters.
60
60
*/
61
- private final Map <Type , DateTimeFormatter > formatters = new HashMap <>();
61
+ private final Map <Type , DateTimeFormatter > formatters = new EnumMap <>(Type . class );
62
62
63
63
/**
64
64
* Factories used when specific formatters have not been specified.
65
65
*/
66
- private final Map <Type , DateTimeFormatterFactory > factories ;
66
+ private final Map <Type , DateTimeFormatterFactory > factories = new EnumMap <>( Type . class ) ;
67
67
68
68
69
69
public DateTimeFormatterRegistrar () {
70
- this .factories = new HashMap <>();
71
70
for (Type type : Type .values ()) {
72
71
this .factories .put (type , new DateTimeFormatterFactory ());
73
72
}
@@ -155,33 +154,38 @@ public void setDateTimeFormatter(DateTimeFormatter formatter) {
155
154
public void registerFormatters (FormatterRegistry registry ) {
156
155
DateTimeConverters .registerConverters (registry );
157
156
158
- DateTimeFormatter dateFormatter = getFormatter (Type .DATE );
159
- DateTimeFormatter timeFormatter = getFormatter (Type .TIME );
160
- DateTimeFormatter dateTimeFormatter = getFormatter (Type .DATE_TIME );
157
+ DateTimeFormatter df = getFormatter (Type .DATE );
158
+ DateTimeFormatter tf = getFormatter (Type .TIME );
159
+ DateTimeFormatter dtf = getFormatter (Type .DATE_TIME );
160
+
161
+ // Efficient ISO_LOCAL_* variants for printing since they are twice as fast...
161
162
162
163
registry .addFormatterForFieldType (LocalDate .class ,
163
- new TemporalAccessorPrinter (dateFormatter ),
164
- new TemporalAccessorParser (LocalDate .class , dateFormatter ));
164
+ new TemporalAccessorPrinter (
165
+ df == DateTimeFormatter .ISO_DATE ? DateTimeFormatter .ISO_LOCAL_DATE : df ),
166
+ new TemporalAccessorParser (LocalDate .class , df ));
165
167
166
168
registry .addFormatterForFieldType (LocalTime .class ,
167
- new TemporalAccessorPrinter (timeFormatter ),
168
- new TemporalAccessorParser (LocalTime .class , timeFormatter ));
169
+ new TemporalAccessorPrinter (
170
+ tf == DateTimeFormatter .ISO_TIME ? DateTimeFormatter .ISO_LOCAL_TIME : tf ),
171
+ new TemporalAccessorParser (LocalTime .class , tf ));
169
172
170
173
registry .addFormatterForFieldType (LocalDateTime .class ,
171
- new TemporalAccessorPrinter (dateTimeFormatter ),
172
- new TemporalAccessorParser (LocalDateTime .class , dateTimeFormatter ));
174
+ new TemporalAccessorPrinter (
175
+ dtf == DateTimeFormatter .ISO_DATE_TIME ? DateTimeFormatter .ISO_LOCAL_DATE_TIME : dtf ),
176
+ new TemporalAccessorParser (LocalDateTime .class , dtf ));
173
177
174
178
registry .addFormatterForFieldType (ZonedDateTime .class ,
175
- new TemporalAccessorPrinter (dateTimeFormatter ),
176
- new TemporalAccessorParser (ZonedDateTime .class , dateTimeFormatter ));
179
+ new TemporalAccessorPrinter (dtf ),
180
+ new TemporalAccessorParser (ZonedDateTime .class , dtf ));
177
181
178
182
registry .addFormatterForFieldType (OffsetDateTime .class ,
179
- new TemporalAccessorPrinter (dateTimeFormatter ),
180
- new TemporalAccessorParser (OffsetDateTime .class , dateTimeFormatter ));
183
+ new TemporalAccessorPrinter (dtf ),
184
+ new TemporalAccessorParser (OffsetDateTime .class , dtf ));
181
185
182
186
registry .addFormatterForFieldType (OffsetTime .class ,
183
- new TemporalAccessorPrinter (timeFormatter ),
184
- new TemporalAccessorParser (OffsetTime .class , timeFormatter ));
187
+ new TemporalAccessorPrinter (tf ),
188
+ new TemporalAccessorParser (OffsetTime .class , tf ));
185
189
186
190
registry .addFormatterForFieldType (Instant .class , new InstantFormatter ());
187
191
registry .addFormatterForFieldType (Period .class , new PeriodFormatter ());
0 commit comments