Skip to content

Enable users to provide custom time and datetime formats for MVC and WebFlux type conversion #18772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,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 {
Expand Down Expand Up @@ -95,6 +112,17 @@ private void registerJsr310() {
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);
}

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

private static String getNonEmptyFormat(final String format) {
return StringUtils.hasText(format) ? format : null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ public EnableWebFluxConfiguration(WebFluxProperties webFluxProperties,
@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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,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.
*/
Expand All @@ -45,6 +55,22 @@ public void setDateFormat(String dateFormat) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,8 @@ private boolean isReadable(Resource resource) {
@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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,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.
*/
Expand Down Expand Up @@ -140,6 +150,22 @@ public void setDateFormat(String dateFormat) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,31 @@ void customDateFormatWithJavaTime() {
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");
Expand Down