2929import java .time .ZonedDateTime ;
3030import java .time .format .DateTimeFormatter ;
3131import java .time .format .FormatStyle ;
32- import java .util .HashMap ;
32+ import java .util .EnumMap ;
3333import java .util .Map ;
3434
3535import org .springframework .format .FormatterRegistrar ;
@@ -56,18 +56,17 @@ private enum Type {DATE, TIME, DATE_TIME}
5656
5757
5858 /**
59- * User defined formatters.
59+ * User- defined formatters.
6060 */
61- private final Map <Type , DateTimeFormatter > formatters = new HashMap <>();
61+ private final Map <Type , DateTimeFormatter > formatters = new EnumMap <>(Type . class );
6262
6363 /**
6464 * Factories used when specific formatters have not been specified.
6565 */
66- private final Map <Type , DateTimeFormatterFactory > factories ;
66+ private final Map <Type , DateTimeFormatterFactory > factories = new EnumMap <>( Type . class ) ;
6767
6868
6969 public DateTimeFormatterRegistrar () {
70- this .factories = new HashMap <>();
7170 for (Type type : Type .values ()) {
7271 this .factories .put (type , new DateTimeFormatterFactory ());
7372 }
@@ -155,33 +154,38 @@ public void setDateTimeFormatter(DateTimeFormatter formatter) {
155154 public void registerFormatters (FormatterRegistry registry ) {
156155 DateTimeConverters .registerConverters (registry );
157156
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...
161162
162163 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 ));
165167
166168 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 ));
169172
170173 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 ));
173177
174178 registry .addFormatterForFieldType (ZonedDateTime .class ,
175- new TemporalAccessorPrinter (dateTimeFormatter ),
176- new TemporalAccessorParser (ZonedDateTime .class , dateTimeFormatter ));
179+ new TemporalAccessorPrinter (dtf ),
180+ new TemporalAccessorParser (ZonedDateTime .class , dtf ));
177181
178182 registry .addFormatterForFieldType (OffsetDateTime .class ,
179- new TemporalAccessorPrinter (dateTimeFormatter ),
180- new TemporalAccessorParser (OffsetDateTime .class , dateTimeFormatter ));
183+ new TemporalAccessorPrinter (dtf ),
184+ new TemporalAccessorParser (OffsetDateTime .class , dtf ));
181185
182186 registry .addFormatterForFieldType (OffsetTime .class ,
183- new TemporalAccessorPrinter (timeFormatter ),
184- new TemporalAccessorParser (OffsetTime .class , timeFormatter ));
187+ new TemporalAccessorPrinter (tf ),
188+ new TemporalAccessorParser (OffsetTime .class , tf ));
185189
186190 registry .addFormatterForFieldType (Instant .class , new InstantFormatter ());
187191 registry .addFormatterForFieldType (Period .class , new PeriodFormatter ());
0 commit comments