JodaTimeFormatterRegistrar supports Duration and Period as well

Issue: SPR-6706
This commit is contained in:
Juergen Hoeller
2015-11-25 23:10:32 +01:00
parent fd84262e71
commit bc0b707175
4 changed files with 140 additions and 3 deletions

View File

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

View File

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

View File

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