diff --git a/spring-context/src/main/java/org/springframework/format/datetime/joda/DurationFormatter.java b/spring-context/src/main/java/org/springframework/format/datetime/joda/DurationFormatter.java new file mode 100644 index 0000000000..96b749dbfc --- /dev/null +++ b/spring-context/src/main/java/org/springframework/format/datetime/joda/DurationFormatter.java @@ -0,0 +1,46 @@ +/* + * 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.joda; + +import java.text.ParseException; +import java.util.Locale; + +import org.joda.time.Duration; + +import org.springframework.format.Formatter; + +/** + * {@link Formatter} implementation for a Joda-Time {@link Duration}, + * following Joda-Time's parsing rules for a Duration. + * + * @author Juergen Hoeller + * @since 4.2.4 + * @see Duration#parse + */ +public class DurationFormatter implements Formatter { + + @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/joda/JodaTimeFormatterRegistrar.java b/spring-context/src/main/java/org/springframework/format/datetime/joda/JodaTimeFormatterRegistrar.java index d3e809afa4..19e5cd0c3b 100644 --- a/spring-context/src/main/java/org/springframework/format/datetime/joda/JodaTimeFormatterRegistrar.java +++ b/spring-context/src/main/java/org/springframework/format/datetime/joda/JodaTimeFormatterRegistrar.java @@ -22,9 +22,11 @@ import java.util.HashMap; import java.util.Map; import org.joda.time.DateTime; +import org.joda.time.Duration; import org.joda.time.LocalDate; import org.joda.time.LocalDateTime; import org.joda.time.LocalTime; +import org.joda.time.Period; import org.joda.time.ReadableInstant; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; @@ -119,9 +121,9 @@ public class JodaTimeFormatterRegistrar implements FormatterRegistrar { * the {@link #setDateStyle(String) dateStyle} and * {@link #setUseIsoFormat(boolean) useIsoFormat} properties will be ignored. * @param formatter the formatter to use + * @since 3.2 * @see #setTimeFormatter * @see #setDateTimeFormatter - * @since 3.2 */ public void setDateFormatter(DateTimeFormatter formatter) { this.formatters.put(Type.DATE, formatter); @@ -133,9 +135,9 @@ public class JodaTimeFormatterRegistrar implements FormatterRegistrar { * the {@link #setTimeStyle(String) timeStyle} and * {@link #setUseIsoFormat(boolean) useIsoFormat} properties will be ignored. * @param formatter the formatter to use + * @since 3.2 * @see #setDateFormatter * @see #setDateTimeFormatter - * @since 3.2 */ public void setTimeFormatter(DateTimeFormatter formatter) { this.formatters.put(Type.TIME, formatter); @@ -148,9 +150,9 @@ public class JodaTimeFormatterRegistrar implements FormatterRegistrar { * the {@link #setDateTimeStyle(String) dateTimeStyle} and * {@link #setUseIsoFormat(boolean) useIsoFormat} properties will be ignored. * @param formatter the formatter to use + * @since 3.2 * @see #setDateFormatter * @see #setTimeFormatter - * @since 3.2 */ public void setDateTimeFormatter(DateTimeFormatter formatter) { this.formatters.put(Type.DATE_TIME, formatter); @@ -194,6 +196,9 @@ public class JodaTimeFormatterRegistrar implements FormatterRegistrar { Date.class, Calendar.class); } + registry.addFormatterForFieldType(Period.class, new PeriodFormatter()); + registry.addFormatterForFieldType(Duration.class, new DurationFormatter()); + registry.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory()); } diff --git a/spring-context/src/main/java/org/springframework/format/datetime/joda/PeriodFormatter.java b/spring-context/src/main/java/org/springframework/format/datetime/joda/PeriodFormatter.java new file mode 100644 index 0000000000..1dab115c36 --- /dev/null +++ b/spring-context/src/main/java/org/springframework/format/datetime/joda/PeriodFormatter.java @@ -0,0 +1,46 @@ +/* + * 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.joda; + +import java.text.ParseException; +import java.util.Locale; + +import org.joda.time.Period; + +import org.springframework.format.Formatter; + +/** + * {@link Formatter} implementation for a Joda-Time {@link Period}, + * following Joda-Time's parsing rules for a Period. + * + * @author Juergen Hoeller + * @since 4.2.4 + * @see Period#parse + */ +public class PeriodFormatter implements Formatter { + + @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/test/java/org/springframework/format/datetime/joda/JodaTimeFormattingTests.java b/spring-context/src/test/java/org/springframework/format/datetime/joda/JodaTimeFormattingTests.java index ec63913b4e..b8e1db83e7 100644 --- a/spring-context/src/test/java/org/springframework/format/datetime/joda/JodaTimeFormattingTests.java +++ b/spring-context/src/test/java/org/springframework/format/datetime/joda/JodaTimeFormattingTests.java @@ -24,10 +24,12 @@ import java.util.Locale; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; +import org.joda.time.Duration; import org.joda.time.Instant; import org.joda.time.LocalDate; import org.joda.time.LocalDateTime; import org.joda.time.LocalTime; +import org.joda.time.Period; import org.joda.time.chrono.ISOChronology; import org.junit.After; import org.junit.Before; @@ -457,6 +459,24 @@ public class JodaTimeFormattingTests { assertNotNull(date); } + @Test + public void testBindPeriod() { + MutablePropertyValues propertyValues = new MutablePropertyValues(); + propertyValues.add("period", "P6Y3M1D"); + binder.bind(propertyValues); + assertEquals(0, binder.getBindingResult().getErrorCount()); + assertTrue(binder.getBindingResult().getFieldValue("period").toString().equals("P6Y3M1D")); + } + + @Test + public void testBindDuration() { + MutablePropertyValues propertyValues = new MutablePropertyValues(); + propertyValues.add("duration", "PT72.345S"); + binder.bind(propertyValues); + assertEquals(0, binder.getBindingResult().getErrorCount()); + assertTrue(binder.getBindingResult().getFieldValue("duration").toString().equals("PT72.345S")); + } + @SuppressWarnings("unused") private static class JodaTimeBean { @@ -515,6 +535,10 @@ public class JodaTimeFormattingTests { @DateTimeFormat(iso=ISO.DATE_TIME) private Instant mutableDateTimeAnnotated; + private Period period; + + private Duration duration; + private final List children = new ArrayList(); public LocalDate getLocalDate() { @@ -678,6 +702,22 @@ public class JodaTimeFormattingTests { this.mutableDateTimeAnnotated = mutableDateTimeAnnotated; } + public Period getPeriod() { + return period; + } + + public void setPeriod(Period period) { + this.period = period; + } + + public Duration getDuration() { + return duration; + } + + public void setDuration(Duration duration) { + this.duration = duration; + } + public List getChildren() { return children; }