Merge pull request #21630 from gaurav-91
* gh-21630: Polish "Support iso-offset for date-time and time formatting with MVC" Support iso-offset for date-time and time formatting with MVC Closes gh-21630
This commit is contained in:
@@ -25,6 +25,7 @@ import org.springframework.util.StringUtils;
|
||||
* {@link DateTimeFormatter Formatters} for dates, times, and date-times.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Gaurav Pareek
|
||||
* @since 2.3.0
|
||||
*/
|
||||
public class DateTimeFormatters {
|
||||
@@ -60,7 +61,8 @@ public class DateTimeFormatters {
|
||||
* @return {@code this} for chained method invocation
|
||||
*/
|
||||
public DateTimeFormatters timeFormat(String pattern) {
|
||||
this.timeFormatter = isIso(pattern) ? DateTimeFormatter.ISO_LOCAL_TIME : formatter(pattern);
|
||||
this.timeFormatter = isIso(pattern) ? DateTimeFormatter.ISO_LOCAL_TIME
|
||||
: (isIsoOffset(pattern) ? DateTimeFormatter.ISO_OFFSET_TIME : formatter(pattern));
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -70,7 +72,8 @@ public class DateTimeFormatters {
|
||||
* @return {@code this} for chained method invocation
|
||||
*/
|
||||
public DateTimeFormatters dateTimeFormat(String pattern) {
|
||||
this.dateTimeFormatter = isIso(pattern) ? DateTimeFormatter.ISO_LOCAL_DATE_TIME : formatter(pattern);
|
||||
this.dateTimeFormatter = isIso(pattern) ? DateTimeFormatter.ISO_LOCAL_DATE_TIME
|
||||
: (isIsoOffset(pattern) ? DateTimeFormatter.ISO_OFFSET_DATE_TIME : formatter(pattern));
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -103,4 +106,8 @@ public class DateTimeFormatters {
|
||||
return "iso".equalsIgnoreCase(pattern);
|
||||
}
|
||||
|
||||
private static boolean isIsoOffset(String pattern) {
|
||||
return "isooffset".equalsIgnoreCase(pattern) || "iso-offset".equalsIgnoreCase(pattern);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1964,6 +1964,10 @@
|
||||
{
|
||||
"value": "iso",
|
||||
"description": "ISO-8601 extended local date-time format."
|
||||
},
|
||||
{
|
||||
"value": "iso-offset",
|
||||
"description": "ISO offset date-time format."
|
||||
}
|
||||
],
|
||||
"providers": [
|
||||
@@ -1982,6 +1986,10 @@
|
||||
{
|
||||
"value": "iso",
|
||||
"description": "ISO-8601 extended local time format"
|
||||
},
|
||||
{
|
||||
"value": "iso-offset",
|
||||
"description": "ISO offset time format."
|
||||
}
|
||||
],
|
||||
"providers": [
|
||||
|
||||
@@ -19,7 +19,10 @@ package org.springframework.boot.autoconfigure.web.format;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.OffsetTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.FormatStyle;
|
||||
@@ -35,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @author Madhura Bhave
|
||||
* @author Gaurav Pareek
|
||||
*/
|
||||
class WebConversionServiceTests {
|
||||
|
||||
@@ -80,6 +84,23 @@ class WebConversionServiceTests {
|
||||
.isEqualTo(DateTimeFormatter.ISO_LOCAL_TIME.format(time));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isoOffsetTimeFormat() {
|
||||
isoOffsetTimeFormat(new DateTimeFormatters().timeFormat("isooffset"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void hyphenatedIsoOffsetTimeFormat() {
|
||||
isoOffsetTimeFormat(new DateTimeFormatters().timeFormat("iso-offset"));
|
||||
}
|
||||
|
||||
private void isoOffsetTimeFormat(DateTimeFormatters formatters) {
|
||||
WebConversionService conversionService = new WebConversionService(formatters);
|
||||
OffsetTime offsetTime = OffsetTime.of(LocalTime.of(12, 45, 23), ZoneOffset.ofHoursMinutes(1, 30));
|
||||
assertThat(conversionService.convert(offsetTime, String.class))
|
||||
.isEqualTo(DateTimeFormatter.ISO_OFFSET_TIME.format(offsetTime));
|
||||
}
|
||||
|
||||
@Test
|
||||
void customTimeFormat() {
|
||||
WebConversionService conversionService = new WebConversionService(
|
||||
@@ -105,6 +126,24 @@ class WebConversionServiceTests {
|
||||
.isEqualTo(DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(dateTime));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isoOffsetDateTimeFormat() {
|
||||
isoOffsetDateTimeFormat(new DateTimeFormatters().dateTimeFormat("isooffset"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void hyphenatedIsoOffsetDateTimeFormat() {
|
||||
isoOffsetDateTimeFormat(new DateTimeFormatters().dateTimeFormat("iso-offset"));
|
||||
}
|
||||
|
||||
private void isoOffsetDateTimeFormat(DateTimeFormatters formatters) {
|
||||
WebConversionService conversionService = new WebConversionService(formatters);
|
||||
OffsetDateTime offsetdate = OffsetDateTime.of(LocalDate.of(2020, 4, 26), LocalTime.of(12, 45, 23),
|
||||
ZoneOffset.ofHoursMinutes(1, 30));
|
||||
assertThat(conversionService.convert(offsetdate, String.class))
|
||||
.isEqualTo(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(offsetdate));
|
||||
}
|
||||
|
||||
@Test
|
||||
void customDateTimeFormat() {
|
||||
WebConversionService conversionService = new WebConversionService(
|
||||
|
||||
Reference in New Issue
Block a user