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
This commit is contained in:
parent
6921fdacac
commit
69b51cd64d
|
@ -48,15 +48,32 @@ public class WebConversionService extends DefaultFormattingConversionService {
|
|||
|
||||
private final String dateFormat;
|
||||
|
||||
private final String timeFormat;
|
||||
|
||||
private final String dateTimeFormat;
|
||||
|
||||
/**
|
||||
* Create a new WebConversionService that configures formatters with the provided date
|
||||
* format, or register the default ones if no custom format is provided.
|
||||
* @param dateFormat the custom date format to use for date conversions
|
||||
*/
|
||||
public WebConversionService(String dateFormat) {
|
||||
this(dateFormat, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new WebConversionService that configures formatters with the provided date
|
||||
* and time formats, or register the default ones if no custom formats are provided.
|
||||
* @param dateFormat the custom date format to use for date conversions
|
||||
* @param timeFormat the custom time format to use for time conversions
|
||||
* @param dateTimeFormat the custom datetime format to use for datetime conversions
|
||||
*/
|
||||
public WebConversionService(String dateFormat, String timeFormat, String dateTimeFormat) {
|
||||
super(false);
|
||||
this.dateFormat = StringUtils.hasText(dateFormat) ? dateFormat : null;
|
||||
if (this.dateFormat != null) {
|
||||
this.dateFormat = getNonEmptyFormat(dateFormat);
|
||||
this.timeFormat = getNonEmptyFormat(timeFormat);
|
||||
this.dateTimeFormat = getNonEmptyFormat(dateTimeFormat);
|
||||
if (this.dateFormat != null || this.timeFormat != null || this.dateTimeFormat != null) {
|
||||
addFormatters();
|
||||
}
|
||||
else {
|
||||
|
@ -81,6 +98,17 @@ public class WebConversionService extends DefaultFormattingConversionService {
|
|||
dateTime.setDateFormatter(
|
||||
DateTimeFormatter.ofPattern(this.dateFormat).withResolverStyle(ResolverStyle.SMART));
|
||||
}
|
||||
|
||||
if (this.timeFormat != null) {
|
||||
dateTime.setTimeFormatter(
|
||||
DateTimeFormatter.ofPattern(this.timeFormat).withResolverStyle(ResolverStyle.SMART));
|
||||
}
|
||||
|
||||
if (this.dateTimeFormat != null) {
|
||||
dateTime.setDateTimeFormatter(
|
||||
DateTimeFormatter.ofPattern(this.dateTimeFormat).withResolverStyle(ResolverStyle.SMART));
|
||||
}
|
||||
|
||||
dateTime.registerFormatters(this);
|
||||
}
|
||||
|
||||
|
@ -93,4 +121,8 @@ public class WebConversionService extends DefaultFormattingConversionService {
|
|||
dateFormatterRegistrar.registerFormatters(this);
|
||||
}
|
||||
|
||||
private static String getNonEmptyFormat(final String format) {
|
||||
return StringUtils.hasText(format) ? format : null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -203,7 +203,8 @@ public class WebFluxAutoConfiguration {
|
|||
@Bean
|
||||
@Override
|
||||
public FormattingConversionService webFluxConversionService() {
|
||||
WebConversionService conversionService = new WebConversionService(this.webFluxProperties.getDateFormat());
|
||||
WebConversionService conversionService = new WebConversionService(this.webFluxProperties.getDateFormat(),
|
||||
this.webFluxProperties.getTimeFormat(), this.webFluxProperties.getDateTimeFormat());
|
||||
addFormatters(conversionService);
|
||||
return conversionService;
|
||||
}
|
||||
|
|
|
@ -38,6 +38,16 @@ public class WebFluxProperties {
|
|||
*/
|
||||
private String dateFormat;
|
||||
|
||||
/**
|
||||
* Time format to use. For instance, `HH:mm:ss`.
|
||||
*/
|
||||
private String timeFormat;
|
||||
|
||||
/**
|
||||
* Datetime format to use. For instance, `yyyy-MM-dd HH:mm:ss`.
|
||||
*/
|
||||
private String dateTimeFormat;
|
||||
|
||||
/**
|
||||
* Path pattern used for static resources.
|
||||
*/
|
||||
|
@ -72,6 +82,22 @@ public class WebFluxProperties {
|
|||
this.dateFormat = dateFormat;
|
||||
}
|
||||
|
||||
public String getTimeFormat() {
|
||||
return this.timeFormat;
|
||||
}
|
||||
|
||||
public void setTimeFormat(final String timeFormat) {
|
||||
this.timeFormat = timeFormat;
|
||||
}
|
||||
|
||||
public String getDateTimeFormat() {
|
||||
return this.dateTimeFormat;
|
||||
}
|
||||
|
||||
public void setDateTimeFormat(final String dateTimeFormat) {
|
||||
this.dateTimeFormat = dateTimeFormat;
|
||||
}
|
||||
|
||||
public String getStaticPathPattern() {
|
||||
return this.staticPathPattern;
|
||||
}
|
||||
|
|
|
@ -427,7 +427,8 @@ public class WebMvcAutoConfiguration {
|
|||
@Bean
|
||||
@Override
|
||||
public FormattingConversionService mvcConversionService() {
|
||||
WebConversionService conversionService = new WebConversionService(this.mvcProperties.getDateFormat());
|
||||
WebConversionService conversionService = new WebConversionService(this.mvcProperties.getDateFormat(),
|
||||
this.mvcProperties.getTimeFormat(), this.mvcProperties.getDateTimeFormat());
|
||||
addFormatters(conversionService);
|
||||
return conversionService;
|
||||
}
|
||||
|
|
|
@ -61,6 +61,16 @@ public class WebMvcProperties {
|
|||
*/
|
||||
private String dateFormat;
|
||||
|
||||
/**
|
||||
* Time format to use. For instance, `HH:mm:ss`.
|
||||
*/
|
||||
private String timeFormat;
|
||||
|
||||
/**
|
||||
* Datetime format to use. For instance, `yyyy-MM-dd HH:mm:ss`.
|
||||
*/
|
||||
private String dateTimeFormat;
|
||||
|
||||
/**
|
||||
* Whether to dispatch TRACE requests to the FrameworkServlet doService method.
|
||||
*/
|
||||
|
@ -147,6 +157,22 @@ public class WebMvcProperties {
|
|||
this.dateFormat = dateFormat;
|
||||
}
|
||||
|
||||
public String getTimeFormat() {
|
||||
return this.timeFormat;
|
||||
}
|
||||
|
||||
public void setTimeFormat(final String timeFormat) {
|
||||
this.timeFormat = timeFormat;
|
||||
}
|
||||
|
||||
public String getDateTimeFormat() {
|
||||
return this.dateTimeFormat;
|
||||
}
|
||||
|
||||
public void setDateTimeFormat(final String dateTimeFormat) {
|
||||
this.dateTimeFormat = dateTimeFormat;
|
||||
}
|
||||
|
||||
public boolean isIgnoreDefaultModelOnRedirect() {
|
||||
return this.ignoreDefaultModelOnRedirect;
|
||||
}
|
||||
|
|
|
@ -42,11 +42,31 @@ class WebConversionServiceTests {
|
|||
customDateFormat(java.time.LocalDate.of(2018, 1, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void customTimeFormatWithJavaTime() {
|
||||
customTimeFormat(java.time.LocalTime.of(13, 37, 42));
|
||||
}
|
||||
|
||||
@Test
|
||||
void customDateTimeFormatWithJavaTime() {
|
||||
customDateTimeFormat(java.time.LocalDateTime.of(2019, 10, 28, 13, 37, 42));
|
||||
}
|
||||
|
||||
private void customDateFormat(Object input) {
|
||||
WebConversionService conversionService = new WebConversionService("dd*MM*yyyy");
|
||||
assertThat(conversionService.convert(input, String.class)).isEqualTo("01*01*2018");
|
||||
}
|
||||
|
||||
private void customTimeFormat(Object input) {
|
||||
WebConversionService conversionService = new WebConversionService(null, "HH*mm*ss", null);
|
||||
assertThat(conversionService.convert(input, String.class)).isEqualTo("13*37*42");
|
||||
}
|
||||
|
||||
private void customDateTimeFormat(Object input) {
|
||||
WebConversionService conversionService = new WebConversionService(null, null, "dd*MM*yyyy HH*mm*ss");
|
||||
assertThat(conversionService.convert(input, String.class)).isEqualTo("28*10*2019 13*37*42");
|
||||
}
|
||||
|
||||
@Test
|
||||
void convertFromStringToDate() {
|
||||
WebConversionService conversionService = new WebConversionService("yyyy-MM-dd");
|
||||
|
|
Loading…
Reference in New Issue