DateTimeFormatterRegistrar supports Duration and Period as well
Issue: SPR-13721
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -85,6 +85,7 @@ final class DateTimeConverters {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@UsesJava8
|
||||
private static class LocalDateTimeToLocalDateConverter implements Converter<LocalDateTime, LocalDate> {
|
||||
|
||||
@@ -94,6 +95,7 @@ final class DateTimeConverters {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@UsesJava8
|
||||
private static class LocalDateTimeToLocalTimeConverter implements Converter<LocalDateTime, LocalTime> {
|
||||
|
||||
@@ -103,6 +105,7 @@ final class DateTimeConverters {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@UsesJava8
|
||||
private static class ZonedDateTimeToLocalDateConverter implements Converter<ZonedDateTime, LocalDate> {
|
||||
|
||||
@@ -112,6 +115,7 @@ final class DateTimeConverters {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@UsesJava8
|
||||
private static class ZonedDateTimeToLocalTimeConverter implements Converter<ZonedDateTime, LocalTime> {
|
||||
|
||||
@@ -121,6 +125,7 @@ final class DateTimeConverters {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@UsesJava8
|
||||
private static class ZonedDateTimeToLocalDateTimeConverter implements Converter<ZonedDateTime, LocalDateTime> {
|
||||
|
||||
@@ -139,6 +144,7 @@ final class DateTimeConverters {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@UsesJava8
|
||||
private static class ZonedDateTimeToInstantConverter implements Converter<ZonedDateTime, Instant> {
|
||||
|
||||
@@ -149,6 +155,7 @@ final class DateTimeConverters {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@UsesJava8
|
||||
private static class OffsetDateTimeToLocalDateConverter implements Converter<OffsetDateTime, LocalDate> {
|
||||
|
||||
@@ -158,6 +165,7 @@ final class DateTimeConverters {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@UsesJava8
|
||||
private static class OffsetDateTimeToLocalTimeConverter implements Converter<OffsetDateTime, LocalTime> {
|
||||
|
||||
@@ -167,6 +175,7 @@ final class DateTimeConverters {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@UsesJava8
|
||||
private static class OffsetDateTimeToLocalDateTimeConverter implements Converter<OffsetDateTime, LocalDateTime> {
|
||||
|
||||
@@ -176,6 +185,7 @@ final class DateTimeConverters {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@UsesJava8
|
||||
private static class OffsetDateTimeToZonedDateTimeConverter implements Converter<OffsetDateTime, ZonedDateTime> {
|
||||
|
||||
@@ -185,6 +195,7 @@ final class DateTimeConverters {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@UsesJava8
|
||||
private static class OffsetDateTimeToInstantConverter implements Converter<OffsetDateTime, Instant> {
|
||||
|
||||
@@ -194,6 +205,7 @@ final class DateTimeConverters {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@UsesJava8
|
||||
private static class CalendarToZonedDateTimeConverter implements Converter<Calendar, ZonedDateTime> {
|
||||
|
||||
@@ -203,6 +215,7 @@ final class DateTimeConverters {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@UsesJava8
|
||||
private static class CalendarToOffsetDateTimeConverter implements Converter<Calendar, OffsetDateTime> {
|
||||
|
||||
@@ -212,6 +225,7 @@ final class DateTimeConverters {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@UsesJava8
|
||||
private static class CalendarToLocalDateConverter implements Converter<Calendar, LocalDate> {
|
||||
|
||||
@@ -221,6 +235,7 @@ final class DateTimeConverters {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@UsesJava8
|
||||
private static class CalendarToLocalTimeConverter implements Converter<Calendar, LocalTime> {
|
||||
|
||||
@@ -230,6 +245,7 @@ final class DateTimeConverters {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@UsesJava8
|
||||
private static class CalendarToLocalDateTimeConverter implements Converter<Calendar, LocalDateTime> {
|
||||
|
||||
@@ -239,6 +255,7 @@ final class DateTimeConverters {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@UsesJava8
|
||||
private static class CalendarToInstantConverter implements Converter<Calendar, Instant> {
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,12 +16,14 @@
|
||||
|
||||
package org.springframework.format.datetime.standard;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.OffsetTime;
|
||||
import java.time.Period;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.FormatStyle;
|
||||
@@ -182,6 +184,8 @@ public class DateTimeFormatterRegistrar implements FormatterRegistrar {
|
||||
new TemporalAccessorParser(OffsetTime.class, timeFormatter));
|
||||
|
||||
registry.addFormatterForFieldType(Instant.class, new InstantFormatter());
|
||||
registry.addFormatterForFieldType(Period.class, new PeriodFormatter());
|
||||
registry.addFormatterForFieldType(Duration.class, new DurationFormatter());
|
||||
|
||||
registry.addFormatterForFieldAnnotation(new Jsr310DateTimeFormatAnnotationFormatterFactory());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.format.datetime.standard;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.time.Duration;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.springframework.format.Formatter;
|
||||
import org.springframework.lang.UsesJava8;
|
||||
|
||||
/**
|
||||
* {@link Formatter} implementation for a JSR-310 {@link Duration},
|
||||
* following JSR-310's parsing rules for a Duration.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 4.2.4
|
||||
* @see Duration#parse
|
||||
*/
|
||||
@UsesJava8
|
||||
public class DurationFormatter implements Formatter<Duration> {
|
||||
|
||||
@Override
|
||||
public Duration parse(String text, Locale locale) throws ParseException {
|
||||
return Duration.parse(text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String print(Duration object, Locale locale) {
|
||||
return object.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.format.datetime.standard;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.time.Instant;
|
||||
import java.time.Period;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.springframework.format.Formatter;
|
||||
import org.springframework.lang.UsesJava8;
|
||||
|
||||
/**
|
||||
* {@link Formatter} implementation for a JSR-310 {@link Period},
|
||||
* following JSR-310's parsing rules for a Period.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 4.2.4
|
||||
* @see Period#parse
|
||||
*/
|
||||
@UsesJava8
|
||||
public class PeriodFormatter implements Formatter<Period> {
|
||||
|
||||
@Override
|
||||
public Period parse(String text, Locale locale) throws ParseException {
|
||||
return Period.parse(text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String print(Period object, Locale locale) {
|
||||
return object.toString();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user