Skip to content

Commit 816fd9c

Browse files
committed
Use spring.mvc.date-pattern to also configure formatting of java.time.LocalDate.
Initial attempt and basis for discussions on how to fix spring-projectsgh-5523.
1 parent 7cc4410 commit 816fd9c

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616

1717
package org.springframework.boot.autoconfigure.web.servlet;
1818

19+
import java.text.ParseException;
20+
import java.time.LocalDate;
21+
import java.time.format.DateTimeFormatter;
1922
import java.util.Collection;
2023
import java.util.Collections;
2124
import java.util.Date;
2225
import java.util.List;
2326
import java.util.ListIterator;
27+
import java.util.Locale;
2428
import java.util.Map;
2529
import java.util.Map.Entry;
2630

@@ -255,6 +259,26 @@ public Formatter<Date> dateFormatter() {
255259
return new DateFormatter(this.mvcProperties.getDateFormat());
256260
}
257261

262+
@Bean
263+
@ConditionalOnProperty(prefix = "spring.mvc", name = "date-format")
264+
public Formatter<LocalDate> localDateFormatter() {
265+
return new Formatter<LocalDate>() {
266+
267+
@Override
268+
public LocalDate parse(String text, Locale locale) throws ParseException {
269+
return LocalDate.parse(text,
270+
DateTimeFormatter.ofPattern(
271+
WebMvcAutoConfigurationAdapter.this.mvcProperties.getDateFormat(), locale));
272+
}
273+
274+
@Override
275+
public String print(LocalDate object, Locale locale) {
276+
return DateTimeFormatter.ofPattern(
277+
WebMvcAutoConfigurationAdapter.this.mvcProperties.getDateFormat(), locale).format(object);
278+
}
279+
};
280+
}
281+
258282
@Override
259283
public MessageCodesResolver getMessageCodesResolver() {
260284
if (this.mvcProperties.getMessageCodesResolverFormat() != null) {

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.autoconfigure.web.servlet;
1818

19+
import java.time.LocalDate;
1920
import java.util.Date;
2021
import java.util.LinkedHashMap;
2122
import java.util.List;
@@ -367,8 +368,13 @@ public void overrideDateFormat() {
367368
.run((context) -> {
368369
FormattingConversionService conversionService = context
369370
.getBean(FormattingConversionService.class);
371+
370372
Date date = new DateTime(1988, 6, 25, 20, 30).toDate();
371-
assertThat(conversionService.convert(date, String.class))
373+
assertThat(conversionService.convert(date, String.class)).as("java.util.Date")
374+
.isEqualTo("25*06*1988");
375+
376+
LocalDate localDate = LocalDate.of(1988, 6, 25);
377+
assertThat(conversionService.convert(localDate, String.class)).as("java.time.LocalDate")
372378
.isEqualTo("25*06*1988");
373379
});
374380
}

0 commit comments

Comments
 (0)