Skip to content

Commit 9eac7c9

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 9eac7c9

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
@@ -59,15 +59,32 @@ public class WebConversionService extends DefaultFormattingConversionService {
5959

6060
private final String dateFormat;
6161

62+
private final String timeFormat;
63+
64+
private final String dateTimeFormat;
65+
6266
/**
6367
* Create a new WebConversionService that configures formatters with the provided date
6468
* format, or register the default ones if no custom format is provided.
6569
* @param dateFormat the custom date format to use for date conversions
6670
*/
6771
public WebConversionService(String dateFormat) {
72+
this(dateFormat, null, null);
73+
}
74+
75+
/**
76+
* Create a new WebConversionService that configures formatters with the provided date
77+
* and time formats, or register the default ones if no custom formats are provided.
78+
* @param dateFormat the custom date format to use for date conversions
79+
* @param timeFormat the custom time format to use for time conversions
80+
* @param dateTimeFormat the custom datetime format to use for datetime conversions
81+
*/
82+
public WebConversionService(String dateFormat, String timeFormat, String dateTimeFormat) {
6883
super(false);
69-
this.dateFormat = StringUtils.hasText(dateFormat) ? dateFormat : null;
70-
if (this.dateFormat != null) {
84+
this.dateFormat = getNonEmptyFormat(dateFormat);
85+
this.timeFormat = getNonEmptyFormat(timeFormat);
86+
this.dateTimeFormat = getNonEmptyFormat(dateTimeFormat);
87+
if (this.dateFormat != null || this.timeFormat != null || this.dateTimeFormat != null) {
7188
addFormatters();
7289
}
7390
else {
@@ -95,6 +112,17 @@ private void registerJsr310() {
95112
dateTime.setDateFormatter(
96113
DateTimeFormatter.ofPattern(this.dateFormat).withResolverStyle(ResolverStyle.SMART));
97114
}
115+
116+
if (this.timeFormat != null) {
117+
dateTime.setTimeFormatter(
118+
DateTimeFormatter.ofPattern(this.timeFormat).withResolverStyle(ResolverStyle.SMART));
119+
}
120+
121+
if (this.dateTimeFormat != null) {
122+
dateTime.setDateTimeFormatter(
123+
DateTimeFormatter.ofPattern(this.dateTimeFormat).withResolverStyle(ResolverStyle.SMART));
124+
}
125+
98126
dateTime.registerFormatters(this);
99127
}
100128

@@ -117,4 +145,8 @@ private void registerJavaDate() {
117145
dateFormatterRegistrar.registerFormatters(this);
118146
}
119147

148+
private static String getNonEmptyFormat(final String format) {
149+
return StringUtils.hasText(format) ? format : null;
150+
}
151+
120152
}

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
@@ -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,6 +55,22 @@ public void setDateFormat(String dateFormat) {
4555
this.dateFormat = dateFormat;
4656
}
4757

58+
public String getTimeFormat() {
59+
return this.timeFormat;
60+
}
61+
62+
public void setTimeFormat(final String timeFormat) {
63+
this.timeFormat = timeFormat;
64+
}
65+
66+
public String getDateTimeFormat() {
67+
return this.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
}

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
@@ -421,7 +421,8 @@ 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(this.mvcProperties.getDateFormat(),
425+
this.mvcProperties.getTimeFormat(), this.mvcProperties.getDateTimeFormat());
425426
addFormatters(conversionService);
426427
return conversionService;
427428
}

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 this.timeFormat;
155+
}
156+
157+
public void setTimeFormat(final String timeFormat) {
158+
this.timeFormat = timeFormat;
159+
}
160+
161+
public String getDateTimeFormat() {
162+
return this.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)