forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support DateTimeFormat annotation without Joda
Dependency on Joda Time when using the @DateTimeFormat annotation is now optional. If Joda Time is not present the JDK SimpleDateFormat will be used to parse and print date patterns. If Joda time is present it will always be used in preference to SimpleDateFormat. Issue: SPR-6508
- Loading branch information
Showing
9 changed files
with
806 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
spring-context/src/main/java/org/springframework/format/datetime/DateFormatterRegistrar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright 2002-2012 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; | ||
|
||
import java.util.Calendar; | ||
import java.util.Date; | ||
|
||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.core.convert.converter.ConverterRegistry; | ||
import org.springframework.format.FormatterRegistrar; | ||
import org.springframework.format.FormatterRegistry; | ||
import org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Configures Date formatting for use with Spring. | ||
* <p> | ||
* Designed for direct instantiation but also exposes the static | ||
* {@link #addDateConverters(ConverterRegistry)} utility method for ad hoc use against any | ||
* {@code ConverterRegistry} instance. | ||
* | ||
* @author Phillip Webb | ||
* @since 3.2 | ||
* @see JodaTimeFormatterRegistrar | ||
* @see FormatterRegistrar#registerFormatters | ||
*/ | ||
public class DateFormatterRegistrar implements FormatterRegistrar { | ||
|
||
|
||
private DateFormatter dateFormatter = new DateFormatter(); | ||
|
||
|
||
public void registerFormatters(FormatterRegistry registry) { | ||
addDateConverters(registry); | ||
registry.addFormatter(dateFormatter); | ||
registry.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory()); | ||
} | ||
|
||
/** | ||
* Set the date formatter to register. If not specified default {@link DateFormatter} | ||
* will be used. This method can be used if additional formatter configuration is | ||
* required. | ||
* @param dateFormatter the date formatter | ||
*/ | ||
public void setFormatter(DateFormatter dateFormatter) { | ||
Assert.notNull(dateFormatter,"DateFormatter must not be null"); | ||
this.dateFormatter = dateFormatter; | ||
} | ||
|
||
/** | ||
* Add date converters to the specified registry. | ||
* @param converterRegistry the registry of converters to add to | ||
*/ | ||
public static void addDateConverters(ConverterRegistry converterRegistry) { | ||
converterRegistry.addConverter(new DateToLongConverter()); | ||
converterRegistry.addConverter(new DateToCalendarConverter()); | ||
converterRegistry.addConverter(new CalendarToDateConverter()); | ||
converterRegistry.addConverter(new CalendarToLongConverter()); | ||
converterRegistry.addConverter(new LongToDateConverter()); | ||
converterRegistry.addConverter(new LongToCalendarConverter()); | ||
} | ||
|
||
|
||
private static class DateToLongConverter implements Converter<Date, Long> { | ||
public Long convert(Date source) { | ||
return source.getTime(); | ||
} | ||
} | ||
|
||
|
||
private static class DateToCalendarConverter implements Converter<Date, Calendar> { | ||
public Calendar convert(Date source) { | ||
Calendar calendar = Calendar.getInstance(); | ||
calendar.setTime(source); | ||
return calendar; | ||
} | ||
} | ||
|
||
|
||
private static class CalendarToDateConverter implements Converter<Calendar, Date> { | ||
public Date convert(Calendar source) { | ||
return source.getTime(); | ||
} | ||
} | ||
|
||
|
||
private static class CalendarToLongConverter implements Converter<Calendar, Long> { | ||
public Long convert(Calendar source) { | ||
return source.getTime().getTime(); | ||
} | ||
} | ||
|
||
|
||
private static class LongToDateConverter implements Converter<Long, Date> { | ||
public Date convert(Long source) { | ||
return new Date(source); | ||
} | ||
} | ||
|
||
|
||
private static class LongToCalendarConverter implements Converter<Long, Calendar> { | ||
|
||
private DateToCalendarConverter dateToCalendarConverter = new DateToCalendarConverter(); | ||
|
||
public Calendar convert(Long source) { | ||
return dateToCalendarConverter.convert(new Date(source)); | ||
} | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
...in/java/org/springframework/format/datetime/DateTimeFormatAnnotationFormatterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2002-2012 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; | ||
|
||
import java.util.Calendar; | ||
import java.util.Collections; | ||
import java.util.Date; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import org.springframework.context.EmbeddedValueResolverAware; | ||
import org.springframework.format.AnnotationFormatterFactory; | ||
import org.springframework.format.Formatter; | ||
import org.springframework.format.Parser; | ||
import org.springframework.format.Printer; | ||
import org.springframework.format.annotation.DateTimeFormat; | ||
import org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory; | ||
import org.springframework.util.StringValueResolver; | ||
|
||
/** | ||
* Formats fields annotated with the {@link DateTimeFormat} annotation. | ||
* | ||
* @author Phillip Webb | ||
* @see JodaDateTimeFormatAnnotationFormatterFactory | ||
* @since 3.2 | ||
*/ | ||
public class DateTimeFormatAnnotationFormatterFactory implements | ||
AnnotationFormatterFactory<DateTimeFormat>, EmbeddedValueResolverAware { | ||
|
||
|
||
private static final Set<Class<?>> FIELD_TYPES; | ||
static { | ||
Set<Class<?>> fieldTypes = new HashSet<Class<?>>(); | ||
fieldTypes.add(Date.class); | ||
fieldTypes.add(Calendar.class); | ||
fieldTypes.add(Long.class); | ||
FIELD_TYPES = Collections.unmodifiableSet(fieldTypes); | ||
} | ||
|
||
|
||
private StringValueResolver embeddedValueResolver; | ||
|
||
|
||
public void setEmbeddedValueResolver(StringValueResolver resolver) { | ||
this.embeddedValueResolver = resolver; | ||
} | ||
|
||
public Set<Class<?>> getFieldTypes() { | ||
return FIELD_TYPES; | ||
} | ||
|
||
public Printer<?> getPrinter(DateTimeFormat annotation, Class<?> fieldType) { | ||
return getFormatter(annotation, fieldType); | ||
} | ||
|
||
public Parser<?> getParser(DateTimeFormat annotation, Class<?> fieldType) { | ||
return getFormatter(annotation, fieldType); | ||
} | ||
|
||
protected Formatter<Date> getFormatter(DateTimeFormat annotation, Class<?> fieldType) { | ||
DateFormatter formatter = new DateFormatter(); | ||
formatter.setStylePattern(resolveEmbeddedValue(annotation.style())); | ||
formatter.setIso(annotation.iso()); | ||
formatter.setPattern(resolveEmbeddedValue(annotation.pattern())); | ||
return formatter; | ||
} | ||
|
||
protected String resolveEmbeddedValue(String value) { | ||
return (this.embeddedValueResolver != null ? this.embeddedValueResolver.resolveStringValue(value) : value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.