|
|
|
@ -29,7 +29,7 @@ import java.time.YearMonth; |
|
|
|
import java.time.ZonedDateTime; |
|
|
|
import java.time.ZonedDateTime; |
|
|
|
import java.time.format.DateTimeFormatter; |
|
|
|
import java.time.format.DateTimeFormatter; |
|
|
|
import java.time.format.FormatStyle; |
|
|
|
import java.time.format.FormatStyle; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.EnumMap; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.format.FormatterRegistrar; |
|
|
|
import org.springframework.format.FormatterRegistrar; |
|
|
|
@ -56,18 +56,17 @@ public class DateTimeFormatterRegistrar implements FormatterRegistrar { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* User defined formatters. |
|
|
|
* User-defined formatters. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private final Map<Type, DateTimeFormatter> formatters = new HashMap<>(); |
|
|
|
private final Map<Type, DateTimeFormatter> formatters = new EnumMap<>(Type.class); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Factories used when specific formatters have not been specified. |
|
|
|
* Factories used when specific formatters have not been specified. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private final Map<Type, DateTimeFormatterFactory> factories; |
|
|
|
private final Map<Type, DateTimeFormatterFactory> factories = new EnumMap<>(Type.class); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public DateTimeFormatterRegistrar() { |
|
|
|
public DateTimeFormatterRegistrar() { |
|
|
|
this.factories = new HashMap<>(); |
|
|
|
|
|
|
|
for (Type type : Type.values()) { |
|
|
|
for (Type type : Type.values()) { |
|
|
|
this.factories.put(type, new DateTimeFormatterFactory()); |
|
|
|
this.factories.put(type, new DateTimeFormatterFactory()); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -155,33 +154,38 @@ public class DateTimeFormatterRegistrar implements FormatterRegistrar { |
|
|
|
public void registerFormatters(FormatterRegistry registry) { |
|
|
|
public void registerFormatters(FormatterRegistry registry) { |
|
|
|
DateTimeConverters.registerConverters(registry); |
|
|
|
DateTimeConverters.registerConverters(registry); |
|
|
|
|
|
|
|
|
|
|
|
DateTimeFormatter dateFormatter = getFormatter(Type.DATE); |
|
|
|
DateTimeFormatter df = getFormatter(Type.DATE); |
|
|
|
DateTimeFormatter timeFormatter = getFormatter(Type.TIME); |
|
|
|
DateTimeFormatter tf = getFormatter(Type.TIME); |
|
|
|
DateTimeFormatter dateTimeFormatter = getFormatter(Type.DATE_TIME); |
|
|
|
DateTimeFormatter dtf = getFormatter(Type.DATE_TIME); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Efficient ISO_LOCAL_* variants for printing since they are twice as fast...
|
|
|
|
|
|
|
|
|
|
|
|
registry.addFormatterForFieldType(LocalDate.class, |
|
|
|
registry.addFormatterForFieldType(LocalDate.class, |
|
|
|
new TemporalAccessorPrinter(dateFormatter), |
|
|
|
new TemporalAccessorPrinter( |
|
|
|
new TemporalAccessorParser(LocalDate.class, dateFormatter)); |
|
|
|
df == DateTimeFormatter.ISO_DATE ? DateTimeFormatter.ISO_LOCAL_DATE : df), |
|
|
|
|
|
|
|
new TemporalAccessorParser(LocalDate.class, df)); |
|
|
|
|
|
|
|
|
|
|
|
registry.addFormatterForFieldType(LocalTime.class, |
|
|
|
registry.addFormatterForFieldType(LocalTime.class, |
|
|
|
new TemporalAccessorPrinter(timeFormatter), |
|
|
|
new TemporalAccessorPrinter( |
|
|
|
new TemporalAccessorParser(LocalTime.class, timeFormatter)); |
|
|
|
tf == DateTimeFormatter.ISO_TIME ? DateTimeFormatter.ISO_LOCAL_TIME : tf), |
|
|
|
|
|
|
|
new TemporalAccessorParser(LocalTime.class, tf)); |
|
|
|
|
|
|
|
|
|
|
|
registry.addFormatterForFieldType(LocalDateTime.class, |
|
|
|
registry.addFormatterForFieldType(LocalDateTime.class, |
|
|
|
new TemporalAccessorPrinter(dateTimeFormatter), |
|
|
|
new TemporalAccessorPrinter( |
|
|
|
new TemporalAccessorParser(LocalDateTime.class, dateTimeFormatter)); |
|
|
|
dtf == DateTimeFormatter.ISO_DATE_TIME ? DateTimeFormatter.ISO_LOCAL_DATE_TIME : dtf), |
|
|
|
|
|
|
|
new TemporalAccessorParser(LocalDateTime.class, dtf)); |
|
|
|
|
|
|
|
|
|
|
|
registry.addFormatterForFieldType(ZonedDateTime.class, |
|
|
|
registry.addFormatterForFieldType(ZonedDateTime.class, |
|
|
|
new TemporalAccessorPrinter(dateTimeFormatter), |
|
|
|
new TemporalAccessorPrinter(dtf), |
|
|
|
new TemporalAccessorParser(ZonedDateTime.class, dateTimeFormatter)); |
|
|
|
new TemporalAccessorParser(ZonedDateTime.class, dtf)); |
|
|
|
|
|
|
|
|
|
|
|
registry.addFormatterForFieldType(OffsetDateTime.class, |
|
|
|
registry.addFormatterForFieldType(OffsetDateTime.class, |
|
|
|
new TemporalAccessorPrinter(dateTimeFormatter), |
|
|
|
new TemporalAccessorPrinter(dtf), |
|
|
|
new TemporalAccessorParser(OffsetDateTime.class, dateTimeFormatter)); |
|
|
|
new TemporalAccessorParser(OffsetDateTime.class, dtf)); |
|
|
|
|
|
|
|
|
|
|
|
registry.addFormatterForFieldType(OffsetTime.class, |
|
|
|
registry.addFormatterForFieldType(OffsetTime.class, |
|
|
|
new TemporalAccessorPrinter(timeFormatter), |
|
|
|
new TemporalAccessorPrinter(tf), |
|
|
|
new TemporalAccessorParser(OffsetTime.class, timeFormatter)); |
|
|
|
new TemporalAccessorParser(OffsetTime.class, tf)); |
|
|
|
|
|
|
|
|
|
|
|
registry.addFormatterForFieldType(Instant.class, new InstantFormatter()); |
|
|
|
registry.addFormatterForFieldType(Instant.class, new InstantFormatter()); |
|
|
|
registry.addFormatterForFieldType(Period.class, new PeriodFormatter()); |
|
|
|
registry.addFormatterForFieldType(Period.class, new PeriodFormatter()); |
|
|
|
|