Skip to content

Commit ac3ce48

Browse files
committed
Enable users to provide custom time and datetime formats
Extend WebFlux and WebMvc properties with timeFormat and dateTimeFormat properties to allow users to customize format of LocalTime and LocalDateTime instances.
1 parent 7827d12 commit ac3ce48

File tree

6 files changed

+111
-5
lines changed

6 files changed

+111
-5
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/format/WebConversionService.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,31 @@ public class WebConversionService extends DefaultFormattingConversionService {
5858
private static final Log logger = LogFactory.getLog(WebConversionService.class);
5959

6060
private final String dateFormat;
61+
private final String timeFormat;
62+
private final String dateTimeFormat;
6163

6264
/**
6365
* Create a new WebConversionService that configures formatters with the provided date
6466
* format, or register the default ones if no custom format is provided.
6567
* @param dateFormat the custom date format to use for date conversions
6668
*/
6769
public WebConversionService(String dateFormat) {
70+
this(dateFormat, null, null);
71+
}
72+
73+
/**
74+
* Create a new WebConversionService that configures formatters with the provided date
75+
* and time formats, or register the default ones if no custom formats are provided.
76+
* @param dateFormat the custom date format to use for date conversions
77+
* @param timeFormat the custom time format to use for time conversions
78+
* @param dateTimeFormat the custom datetime format to use for datetime conversions
79+
*/
80+
public WebConversionService(String dateFormat, String timeFormat, String dateTimeFormat) {
6881
super(false);
69-
this.dateFormat = StringUtils.hasText(dateFormat) ? dateFormat : null;
70-
if (this.dateFormat != null) {
82+
this.dateFormat = getNonEmptyFormat(dateFormat);
83+
this.timeFormat = getNonEmptyFormat(timeFormat);
84+
this.dateTimeFormat = getNonEmptyFormat(dateTimeFormat);
85+
if (this.dateFormat != null || this.timeFormat != null || this.dateTimeFormat != null) {
7186
addFormatters();
7287
}
7388
else {
@@ -95,6 +110,17 @@ private void registerJsr310() {
95110
dateTime.setDateFormatter(
96111
DateTimeFormatter.ofPattern(this.dateFormat).withResolverStyle(ResolverStyle.SMART));
97112
}
113+
114+
if (this.timeFormat != null) {
115+
dateTime.setTimeFormatter(
116+
DateTimeFormatter.ofPattern(this.timeFormat).withResolverStyle(ResolverStyle.SMART));
117+
}
118+
119+
if (this.dateTimeFormat != null) {
120+
dateTime.setDateTimeFormatter(
121+
DateTimeFormatter.ofPattern(this.dateTimeFormat).withResolverStyle(ResolverStyle.SMART));
122+
}
123+
98124
dateTime.registerFormatters(this);
99125
}
100126

@@ -117,4 +143,7 @@ private void registerJavaDate() {
117143
dateFormatterRegistrar.registerFormatters(this);
118144
}
119145

146+
private static String getNonEmptyFormat(final String format) {
147+
return StringUtils.hasText(format) ? format : null;
148+
}
120149
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfiguration.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,10 @@ public EnableWebFluxConfiguration(WebFluxProperties webFluxProperties,
203203
@Bean
204204
@Override
205205
public FormattingConversionService webFluxConversionService() {
206-
WebConversionService conversionService = new WebConversionService(this.webFluxProperties.getDateFormat());
206+
WebConversionService conversionService = new WebConversionService(
207+
this.webFluxProperties.getDateFormat(),
208+
this.webFluxProperties.getTimeFormat(),
209+
this.webFluxProperties.getDateTimeFormat());
207210
addFormatters(conversionService);
208211
return conversionService;
209212
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxProperties.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ public class WebFluxProperties {
3232
*/
3333
private String dateFormat;
3434

35+
/**
36+
* Time format to use. For instance, `HH:mm:ss`.
37+
*/
38+
private String timeFormat;
39+
40+
/**
41+
* Datetime format to use. For instance, `yyyy-MM-dd HH:mm:ss`.
42+
*/
43+
private String dateTimeFormat;
44+
3545
/**
3646
* Path pattern used for static resources.
3747
*/
@@ -45,12 +55,27 @@ public void setDateFormat(String dateFormat) {
4555
this.dateFormat = dateFormat;
4656
}
4757

58+
public String getTimeFormat() {
59+
return timeFormat;
60+
}
61+
62+
public void setTimeFormat(final String timeFormat) {
63+
this.timeFormat = timeFormat;
64+
}
65+
66+
public String getDateTimeFormat() {
67+
return dateTimeFormat;
68+
}
69+
70+
public void setDateTimeFormat(final String dateTimeFormat) {
71+
this.dateTimeFormat = dateTimeFormat;
72+
}
73+
4874
public String getStaticPathPattern() {
4975
return this.staticPathPattern;
5076
}
5177

5278
public void setStaticPathPattern(String staticPathPattern) {
5379
this.staticPathPattern = staticPathPattern;
5480
}
55-
5681
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,10 @@ private boolean isReadable(Resource resource) {
421421
@Bean
422422
@Override
423423
public FormattingConversionService mvcConversionService() {
424-
WebConversionService conversionService = new WebConversionService(this.mvcProperties.getDateFormat());
424+
WebConversionService conversionService = new WebConversionService(
425+
this.mvcProperties.getDateFormat(),
426+
this.mvcProperties.getTimeFormat(),
427+
this.mvcProperties.getDateTimeFormat());
425428
addFormatters(conversionService);
426429
return conversionService;
427430
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ public class WebMvcProperties {
6060
*/
6161
private String dateFormat;
6262

63+
/**
64+
* Time format to use. For instance, `HH:mm:ss`.
65+
*/
66+
private String timeFormat;
67+
68+
/**
69+
* Datetime format to use. For instance, `yyyy-MM-dd HH:mm:ss`.
70+
*/
71+
private String dateTimeFormat;
72+
6373
/**
6474
* Whether to dispatch TRACE requests to the FrameworkServlet doService method.
6575
*/
@@ -140,6 +150,22 @@ public void setDateFormat(String dateFormat) {
140150
this.dateFormat = dateFormat;
141151
}
142152

153+
public String getTimeFormat() {
154+
return timeFormat;
155+
}
156+
157+
public void setTimeFormat(final String timeFormat) {
158+
this.timeFormat = timeFormat;
159+
}
160+
161+
public String getDateTimeFormat() {
162+
return dateTimeFormat;
163+
}
164+
165+
public void setDateTimeFormat(final String dateTimeFormat) {
166+
this.dateTimeFormat = dateTimeFormat;
167+
}
168+
143169
public boolean isIgnoreDefaultModelOnRedirect() {
144170
return this.ignoreDefaultModelOnRedirect;
145171
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/format/WebConversionServiceTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,31 @@ void customDateFormatWithJavaTime() {
5050
customDateFormat(java.time.LocalDate.of(2018, 1, 1));
5151
}
5252

53+
@Test
54+
void customTimeFormatWithJavaTime() {
55+
customTimeFormat(java.time.LocalTime.of(13, 37, 42));
56+
}
57+
58+
@Test
59+
void customDateTimeFormatWithJavaTime() {
60+
customDateTimeFormat(java.time.LocalDateTime.of(2019, 10, 28, 13, 37, 42));
61+
}
62+
5363
private void customDateFormat(Object input) {
5464
WebConversionService conversionService = new WebConversionService("dd*MM*yyyy");
5565
assertThat(conversionService.convert(input, String.class)).isEqualTo("01*01*2018");
5666
}
5767

68+
private void customTimeFormat(Object input) {
69+
WebConversionService conversionService = new WebConversionService(null, "HH*mm*ss", null);
70+
assertThat(conversionService.convert(input, String.class)).isEqualTo("13*37*42");
71+
}
72+
73+
private void customDateTimeFormat(Object input) {
74+
WebConversionService conversionService = new WebConversionService(null, null, "dd*MM*yyyy HH*mm*ss");
75+
assertThat(conversionService.convert(input, String.class)).isEqualTo("28*10*2019 13*37*42");
76+
}
77+
5878
@Test
5979
void convertFromStringToDate() {
6080
WebConversionService conversionService = new WebConversionService("yyyy-MM-dd");

0 commit comments

Comments
 (0)