Skip to content

Commit 69b51cd

Browse files
knittlwilkinsona
authored andcommitted
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. See gh-18772
1 parent 6921fda commit 69b51cd

File tree

6 files changed

+110
-4
lines changed

6 files changed

+110
-4
lines changed

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,32 @@ public class WebConversionService extends DefaultFormattingConversionService {
4848

4949
private final String dateFormat;
5050

51+
private final String timeFormat;
52+
53+
private final String dateTimeFormat;
54+
5155
/**
5256
* Create a new WebConversionService that configures formatters with the provided date
5357
* format, or register the default ones if no custom format is provided.
5458
* @param dateFormat the custom date format to use for date conversions
5559
*/
5660
public WebConversionService(String dateFormat) {
61+
this(dateFormat, null, null);
62+
}
63+
64+
/**
65+
* Create a new WebConversionService that configures formatters with the provided date
66+
* and time formats, or register the default ones if no custom formats are provided.
67+
* @param dateFormat the custom date format to use for date conversions
68+
* @param timeFormat the custom time format to use for time conversions
69+
* @param dateTimeFormat the custom datetime format to use for datetime conversions
70+
*/
71+
public WebConversionService(String dateFormat, String timeFormat, String dateTimeFormat) {
5772
super(false);
58-
this.dateFormat = StringUtils.hasText(dateFormat) ? dateFormat : null;
59-
if (this.dateFormat != null) {
73+
this.dateFormat = getNonEmptyFormat(dateFormat);
74+
this.timeFormat = getNonEmptyFormat(timeFormat);
75+
this.dateTimeFormat = getNonEmptyFormat(dateTimeFormat);
76+
if (this.dateFormat != null || this.timeFormat != null || this.dateTimeFormat != null) {
6077
addFormatters();
6178
}
6279
else {
@@ -81,6 +98,17 @@ private void registerJsr310() {
8198
dateTime.setDateFormatter(
8299
DateTimeFormatter.ofPattern(this.dateFormat).withResolverStyle(ResolverStyle.SMART));
83100
}
101+
102+
if (this.timeFormat != null) {
103+
dateTime.setTimeFormatter(
104+
DateTimeFormatter.ofPattern(this.timeFormat).withResolverStyle(ResolverStyle.SMART));
105+
}
106+
107+
if (this.dateTimeFormat != null) {
108+
dateTime.setDateTimeFormatter(
109+
DateTimeFormatter.ofPattern(this.dateTimeFormat).withResolverStyle(ResolverStyle.SMART));
110+
}
111+
84112
dateTime.registerFormatters(this);
85113
}
86114

@@ -93,4 +121,8 @@ private void registerJavaDate() {
93121
dateFormatterRegistrar.registerFormatters(this);
94122
}
95123

124+
private static String getNonEmptyFormat(final String format) {
125+
return StringUtils.hasText(format) ? format : null;
126+
}
127+
96128
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ 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(this.webFluxProperties.getDateFormat(),
207+
this.webFluxProperties.getTimeFormat(), this.webFluxProperties.getDateTimeFormat());
207208
addFormatters(conversionService);
208209
return conversionService;
209210
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ public class WebFluxProperties {
3838
*/
3939
private String dateFormat;
4040

41+
/**
42+
* Time format to use. For instance, `HH:mm:ss`.
43+
*/
44+
private String timeFormat;
45+
46+
/**
47+
* Datetime format to use. For instance, `yyyy-MM-dd HH:mm:ss`.
48+
*/
49+
private String dateTimeFormat;
50+
4151
/**
4252
* Path pattern used for static resources.
4353
*/
@@ -72,6 +82,22 @@ public void setDateFormat(String dateFormat) {
7282
this.dateFormat = dateFormat;
7383
}
7484

85+
public String getTimeFormat() {
86+
return this.timeFormat;
87+
}
88+
89+
public void setTimeFormat(final String timeFormat) {
90+
this.timeFormat = timeFormat;
91+
}
92+
93+
public String getDateTimeFormat() {
94+
return this.dateTimeFormat;
95+
}
96+
97+
public void setDateTimeFormat(final String dateTimeFormat) {
98+
this.dateTimeFormat = dateTimeFormat;
99+
}
100+
75101
public String getStaticPathPattern() {
76102
return this.staticPathPattern;
77103
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,8 @@ private boolean isReadable(Resource resource) {
427427
@Bean
428428
@Override
429429
public FormattingConversionService mvcConversionService() {
430-
WebConversionService conversionService = new WebConversionService(this.mvcProperties.getDateFormat());
430+
WebConversionService conversionService = new WebConversionService(this.mvcProperties.getDateFormat(),
431+
this.mvcProperties.getTimeFormat(), this.mvcProperties.getDateTimeFormat());
431432
addFormatters(conversionService);
432433
return conversionService;
433434
}

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
@@ -61,6 +61,16 @@ public class WebMvcProperties {
6161
*/
6262
private String dateFormat;
6363

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

160+
public String getTimeFormat() {
161+
return this.timeFormat;
162+
}
163+
164+
public void setTimeFormat(final String timeFormat) {
165+
this.timeFormat = timeFormat;
166+
}
167+
168+
public String getDateTimeFormat() {
169+
return this.dateTimeFormat;
170+
}
171+
172+
public void setDateTimeFormat(final String dateTimeFormat) {
173+
this.dateTimeFormat = dateTimeFormat;
174+
}
175+
150176
public boolean isIgnoreDefaultModelOnRedirect() {
151177
return this.ignoreDefaultModelOnRedirect;
152178
}

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
@@ -42,11 +42,31 @@ void customDateFormatWithJavaTime() {
4242
customDateFormat(java.time.LocalDate.of(2018, 1, 1));
4343
}
4444

45+
@Test
46+
void customTimeFormatWithJavaTime() {
47+
customTimeFormat(java.time.LocalTime.of(13, 37, 42));
48+
}
49+
50+
@Test
51+
void customDateTimeFormatWithJavaTime() {
52+
customDateTimeFormat(java.time.LocalDateTime.of(2019, 10, 28, 13, 37, 42));
53+
}
54+
4555
private void customDateFormat(Object input) {
4656
WebConversionService conversionService = new WebConversionService("dd*MM*yyyy");
4757
assertThat(conversionService.convert(input, String.class)).isEqualTo("01*01*2018");
4858
}
4959

60+
private void customTimeFormat(Object input) {
61+
WebConversionService conversionService = new WebConversionService(null, "HH*mm*ss", null);
62+
assertThat(conversionService.convert(input, String.class)).isEqualTo("13*37*42");
63+
}
64+
65+
private void customDateTimeFormat(Object input) {
66+
WebConversionService conversionService = new WebConversionService(null, null, "dd*MM*yyyy HH*mm*ss");
67+
assertThat(conversionService.convert(input, String.class)).isEqualTo("28*10*2019 13*37*42");
68+
}
69+
5070
@Test
5171
void convertFromStringToDate() {
5272
WebConversionService conversionService = new WebConversionService("yyyy-MM-dd");

0 commit comments

Comments
 (0)