diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/format/DateTimeFormatters.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/format/DateTimeFormatters.java index 7e2947d7c1..b41fb27ec2 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/format/DateTimeFormatters.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/format/DateTimeFormatters.java @@ -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); + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 794ffa7656..3a06b15b97 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -1964,6 +1964,10 @@ { "value": "iso", "description": "ISO-8601 extended local date-time format." + }, + { + "value": "isooffset", + "description": "ISO Offset local date-time format." } ], "providers": [ @@ -1982,6 +1986,10 @@ { "value": "iso", "description": "ISO-8601 extended local time format" + }, + { + "value": "isooffset", + "description": "ISO Offset local time format." } ], "providers": [ diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/format/WebConversionServiceTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/format/WebConversionServiceTests.java index c78ee01f2c..d8eb600e13 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/format/WebConversionServiceTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/format/WebConversionServiceTests.java @@ -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 { @@ -76,9 +80,18 @@ class WebConversionServiceTests { void isoTimeFormat() { WebConversionService conversionService = new WebConversionService(new DateTimeFormatters().timeFormat("iso")); LocalTime time = LocalTime.of(12, 45, 23); + System.out.println(conversionService.convert(time, String.class)); assertThat(conversionService.convert(time, String.class)) .isEqualTo(DateTimeFormatter.ISO_LOCAL_TIME.format(time)); } + + @Test + void isoOffsetTimeFormat() { + WebConversionService conversionService = new WebConversionService(new DateTimeFormatters().timeFormat("isooffset")); + 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() { @@ -105,6 +118,15 @@ class WebConversionServiceTests { .isEqualTo(DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(dateTime)); } + @Test + void isoOffsetDateTimeFormat() { + WebConversionService conversionService = new WebConversionService( + new DateTimeFormatters().dateTimeFormat("isooffset")); + 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(