diff options
author | Emmanuel Bourg <ebourg@apache.org> | 2016-08-02 11:13:32 +0200 |
---|---|---|
committer | Emmanuel Bourg <ebourg@apache.org> | 2016-08-02 11:13:32 +0200 |
commit | f69f2a4b8ea697b3a631c0dc7a470e3c9793fee3 (patch) | |
tree | db2f25b29aa3e59c463ab41d3f2856f6265bb1a5 /spring-context/src/main/java/org/springframework/format/datetime/standard | |
parent | 5575b60c30c5a0c308c4ba3a2db93956d8c1746c (diff) |
Imported Upstream version 4.2.6
Diffstat (limited to 'spring-context/src/main/java/org/springframework/format/datetime/standard')
7 files changed, 220 insertions, 7 deletions
diff --git a/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeConverters.java b/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeConverters.java index 0c48e74f..fe5a1162 100644 --- a/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeConverters.java +++ b/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeConverters.java @@ -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> { diff --git a/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterRegistrar.java b/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterRegistrar.java index 3eecedcd..07a65cc9 100644 --- a/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterRegistrar.java +++ b/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterRegistrar.java @@ -16,12 +16,16 @@ 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.MonthDay; import java.time.OffsetDateTime; import java.time.OffsetTime; +import java.time.Period; +import java.time.YearMonth; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; @@ -182,6 +186,10 @@ 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.addFormatterForFieldType(YearMonth.class, new YearMonthFormatter()); + registry.addFormatterForFieldType(MonthDay.class, new MonthDayFormatter()); registry.addFormatterForFieldAnnotation(new Jsr310DateTimeFormatAnnotationFormatterFactory()); } diff --git a/spring-context/src/main/java/org/springframework/format/datetime/standard/DurationFormatter.java b/spring-context/src/main/java/org/springframework/format/datetime/standard/DurationFormatter.java new file mode 100644 index 00000000..57764143 --- /dev/null +++ b/spring-context/src/main/java/org/springframework/format/datetime/standard/DurationFormatter.java @@ -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 +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(); + } + +} diff --git a/spring-context/src/main/java/org/springframework/format/datetime/standard/MonthDayFormatter.java b/spring-context/src/main/java/org/springframework/format/datetime/standard/MonthDayFormatter.java new file mode 100644 index 00000000..41a36f9f --- /dev/null +++ b/spring-context/src/main/java/org/springframework/format/datetime/standard/MonthDayFormatter.java @@ -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.MonthDay; +import java.util.Locale; + +import org.springframework.format.Formatter; +import org.springframework.lang.UsesJava8; + +/** + * {@link Formatter} implementation for a JSR-310 {@link MonthDay}, + * following JSR-310's parsing rules for a MonthDay. + * + * @author Juergen Hoeller + * @since 4.2.4 + * @see MonthDay#parse + */ +@UsesJava8 +class MonthDayFormatter implements Formatter<MonthDay> { + + @Override + public MonthDay parse(String text, Locale locale) throws ParseException { + return MonthDay.parse(text); + } + + @Override + public String print(MonthDay object, Locale locale) { + return object.toString(); + } + +} diff --git a/spring-context/src/main/java/org/springframework/format/datetime/standard/PeriodFormatter.java b/spring-context/src/main/java/org/springframework/format/datetime/standard/PeriodFormatter.java new file mode 100644 index 00000000..8b13574a --- /dev/null +++ b/spring-context/src/main/java/org/springframework/format/datetime/standard/PeriodFormatter.java @@ -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.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 +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(); + } + +} diff --git a/spring-context/src/main/java/org/springframework/format/datetime/standard/TemporalAccessorParser.java b/spring-context/src/main/java/org/springframework/format/datetime/standard/TemporalAccessorParser.java index dc17e306..74eb3648 100644 --- a/spring-context/src/main/java/org/springframework/format/datetime/standard/TemporalAccessorParser.java +++ b/spring-context/src/main/java/org/springframework/format/datetime/standard/TemporalAccessorParser.java @@ -67,22 +67,22 @@ public final class TemporalAccessorParser implements Parser<TemporalAccessor> { @Override public TemporalAccessor parse(String text, Locale locale) throws ParseException { DateTimeFormatter formatterToUse = DateTimeContextHolder.getFormatter(this.formatter, locale); - if (LocalDate.class.equals(this.temporalAccessorType)) { + if (LocalDate.class == this.temporalAccessorType) { return LocalDate.parse(text, formatterToUse); } - else if (LocalTime.class.equals(this.temporalAccessorType)) { + else if (LocalTime.class == this.temporalAccessorType) { return LocalTime.parse(text, formatterToUse); } - else if (LocalDateTime.class.equals(this.temporalAccessorType)) { + else if (LocalDateTime.class == this.temporalAccessorType) { return LocalDateTime.parse(text, formatterToUse); } - else if (ZonedDateTime.class.equals(this.temporalAccessorType)) { + else if (ZonedDateTime.class == this.temporalAccessorType) { return ZonedDateTime.parse(text, formatterToUse); } - else if (OffsetDateTime.class.equals(this.temporalAccessorType)) { + else if (OffsetDateTime.class == this.temporalAccessorType) { return OffsetDateTime.parse(text, formatterToUse); } - else if (OffsetTime.class.equals(this.temporalAccessorType)) { + else if (OffsetTime.class == this.temporalAccessorType) { return OffsetTime.parse(text, formatterToUse); } else { diff --git a/spring-context/src/main/java/org/springframework/format/datetime/standard/YearMonthFormatter.java b/spring-context/src/main/java/org/springframework/format/datetime/standard/YearMonthFormatter.java new file mode 100644 index 00000000..402f77df --- /dev/null +++ b/spring-context/src/main/java/org/springframework/format/datetime/standard/YearMonthFormatter.java @@ -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.YearMonth; +import java.util.Locale; + +import org.springframework.format.Formatter; +import org.springframework.lang.UsesJava8; + +/** + * {@link Formatter} implementation for a JSR-310 {@link YearMonth}, + * following JSR-310's parsing rules for a YearMonth. + * + * @author Juergen Hoeller + * @since 4.2.4 + * @see YearMonth#parse + */ +@UsesJava8 +class YearMonthFormatter implements Formatter<YearMonth> { + + @Override + public YearMonth parse(String text, Locale locale) throws ParseException { + return YearMonth.parse(text); + } + + @Override + public String print(YearMonth object, Locale locale) { + return object.toString(); + } + +} |