Support iso-offset for date-time and time formatting with MVC

See gh-21630
pull/22178/head
Gaurav Pareek 5 years ago committed by Andy Wilkinson
parent bb1a0ffbd5
commit c3b1172aea

@ -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);
}
}

@ -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": [

@ -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(

Loading…
Cancel
Save