Skip to content

Commit b5d44e1

Browse files
committed
Revised Joda-Time support (as a side effect of JSR-310 support in Spring 4.0)
Issue: SPR-9641
1 parent d3a4068 commit b5d44e1

19 files changed

Lines changed: 259 additions & 237 deletions

‎spring-context/src/main/java/org/springframework/format/annotation/DateTimeFormat.java‎

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,22 +23,26 @@
2323

2424
/**
2525
* Declares that a field should be formatted as a date time.
26-
* Supports formatting by style pattern, ISO date time pattern, or custom format pattern string.
27-
* Can be applied to {@code java.util.Date}, {@code java.util.Calendar}, {@code java.long.Long}, or Joda Time fields.
28-
* <p>
29-
* For style-based formatting, set the {@link #style()} attribute to be the style pattern code.
26+
*
27+
* <p>Supports formatting by style pattern, ISO date time pattern, or custom format pattern string.
28+
* Can be applied to {@code java.util.Date}, {@code java.util.Calendar}, {@code java.long.Long},
29+
* Joda-Time value types; and as of Spring 4 and JDK 8, to JSR-310 <code>java.time</code> types too.
30+
*
31+
* <p>For style-based formatting, set the {@link #style()} attribute to be the style pattern code.
3032
* The first character of the code is the date style, and the second character is the time style.
3133
* Specify a character of 'S' for short style, 'M' for medium, 'L' for long, and 'F' for full.
3234
* A date or time may be omitted by specifying the style character '-'.
33-
* <p>
34-
* For ISO-based formatting, set the {@link #iso()} attribute to be the desired {@link ISO} format, such as {@link ISO#DATE}.
35-
<p>
36-
* For custom formatting, set the {@link #pattern()} attribute to be the DateTime pattern, such as {@code yyyy/MM/dd hh:mm:ss a}.
37-
* <p>
38-
* Each attribute is mutually exclusive, so only set one attribute per annotation instance (the one most convenient one for your formatting needs).
35+
*
36+
* <p>For ISO-based formatting, set the {@link #iso()} attribute to be the desired {@link ISO} format,
37+
* such as {@link ISO#DATE}. For custom formatting, set the {@link #pattern()} attribute to be the
38+
* DateTime pattern, such as {@code yyyy/MM/dd hh:mm:ss a}.
39+
*
40+
* <p>Each attribute is mutually exclusive, so only set one attribute per annotation instance
41+
* (the one most convenient one for your formatting needs).
3942
* When the pattern attribute is specified, it takes precedence over both the style and ISO attribute.
4043
* When the iso attribute is specified, if takes precedence over the style attribute.
41-
* When no annotation attributes are specified, the default format applied is style-based with a style code of 'SS' (short date, short time).
44+
* When no annotation attributes are specified, the default format applied is style-based
45+
* with a style code of 'SS' (short date, short time).
4246
*
4347
* @author Keith Donald
4448
* @author Juergen Hoeller
@@ -51,23 +55,24 @@
5155

5256
/**
5357
* The style pattern to use to format the field.
54-
* Defaults to 'SS' for short date time.
55-
* Set this attribute when you wish to format your field in accordance with a common style other than the default style.
58+
* <p>Defaults to 'SS' for short date time. Set this attribute when you wish to format
59+
* your field in accordance with a common style other than the default style.
5660
*/
5761
String style() default "SS";
5862

5963
/**
6064
* The ISO pattern to use to format the field.
6165
* The possible ISO patterns are defined in the {@link ISO} enum.
62-
* Defaults to ISO.NONE, indicating this attribute should be ignored.
63-
* Set this attribute when you wish to format your field in accordance with an ISO date time format.
66+
* <p>Defaults to {@link ISO#NONE}, indicating this attribute should be ignored.
67+
* Set this attribute when you wish to format your field in accordance with an ISO format.
6468
*/
6569
ISO iso() default ISO.NONE;
6670

6771
/**
6872
* The custom pattern to use to format the field.
69-
* Defaults to empty String, indicating no custom pattern String has been specified.
70-
* Set this attribute when you wish to format your field in accordance with a custom date time pattern not represented by a style or ISO format.
73+
* <p>Defaults to empty String, indicating no custom pattern String has been specified.
74+
* Set this attribute when you wish to format your field in accordance with a custom
75+
* date time pattern not represented by a style or ISO format.
7176
*/
7277
String pattern() default "";
7378

@@ -78,18 +83,21 @@
7883
public enum ISO {
7984

8085
/**
81-
* The most common ISO Date Format {@code yyyy-MM-dd} e.g. 2000-10-31.
86+
* The most common ISO Date Format {@code yyyy-MM-dd},
87+
* e.g. 2000-10-31.
8288
*/
8389
DATE,
8490

8591
/**
86-
* The most common ISO Time Format {@code HH:mm:ss.SSSZ} e.g. 01:30:00.000-05:00.
92+
* The most common ISO Time Format {@code HH:mm:ss.SSSZ},
93+
* e.g. 01:30:00.000-05:00.
8794
*/
8895
TIME,
8996

9097
/**
91-
* The most common ISO DateTime Format {@code yyyy-MM-dd'T'HH:mm:ss.SSSZ} e.g. 2000-10-31 01:30:00.000-05:00.
92-
* The default if no annotation value is specified.
98+
* The most common ISO DateTime Format {@code yyyy-MM-dd'T'HH:mm:ss.SSSZ},
99+
* e.g. 2000-10-31 01:30:00.000-05:00.
100+
* <p>This is the default if no annotation value is specified.
93101
*/
94102
DATE_TIME,
95103

‎spring-context/src/main/java/org/springframework/format/datetime/DateFormatter.java‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,7 +46,7 @@ public class DateFormatter implements Formatter<Date> {
4646

4747
private static final Map<ISO, String> ISO_PATTERNS;
4848
static {
49-
Map<ISO, String> formats = new HashMap<DateTimeFormat.ISO, String>();
49+
Map<ISO, String> formats = new HashMap<DateTimeFormat.ISO, String>(4);
5050
formats.put(ISO.DATE, "yyyy-MM-dd");
5151
formats.put(ISO.TIME, "HH:mm:ss.SSSZ");
5252
formats.put(ISO.DATE_TIME, "yyyy-MM-dd'T'HH:mm:ss.SSSZ");
@@ -89,6 +89,15 @@ public void setPattern(String pattern) {
8989
this.pattern = pattern;
9090
}
9191

92+
/**
93+
* Set the ISO format used for this date.
94+
* @param iso the {@link ISO} format
95+
* @since 3.2
96+
*/
97+
public void setIso(ISO iso) {
98+
this.iso = iso;
99+
}
100+
92101
/**
93102
* Set the style to use to format date values.
94103
* <p>If not specified, DateFormat's default style will be used.
@@ -112,22 +121,14 @@ public void setStyle(int style) {
112121
* <li>'F' = Full</li>
113122
* <li>'-' = Omitted</li>
114123
* <ul>
115-
* This method mimics the styles supported by Joda Time.
124+
* This method mimics the styles supported by Joda-Time.
116125
* @param stylePattern two characters from the set {"S", "M", "L", "F", "-"}
117126
* @since 3.2
118127
*/
119128
public void setStylePattern(String stylePattern) {
120129
this.stylePattern = stylePattern;
121130
}
122131

123-
/**
124-
* Set the ISO format used for this date.
125-
* @param iso the {@link ISO} format
126-
* @since 3.2
127-
*/
128-
public void setIso(ISO iso) {
129-
this.iso = iso;
130-
}
131132
/**
132133
* Set the TimeZone to normalize the date values into, if any.
133134
*/
@@ -204,4 +205,5 @@ private int getStylePatternForChar(int index) {
204205
}
205206
throw new IllegalStateException("Unsupported style pattern '"+ stylePattern+ "'");
206207
}
208+
207209
}

‎spring-context/src/main/java/org/springframework/format/datetime/DateFormatterRegistrar.java‎

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727

2828
/**
2929
* Configures Date formatting for use with Spring.
30-
* <p>
31-
* Designed for direct instantiation but also exposes the static
30+
*
31+
* <p>Designed for direct instantiation but also exposes the static
3232
* {@link #addDateConverters(ConverterRegistry)} utility method for ad hoc use
3333
* against any {@code ConverterRegistry} instance.
3434
*
@@ -39,10 +39,20 @@
3939
*/
4040
public class DateFormatterRegistrar implements FormatterRegistrar {
4141

42-
4342
private DateFormatter dateFormatter;
4443

4544

45+
/**
46+
* Set the date formatter to register. If not specified no formatter is registered.
47+
* This method can be used if global formatter configuration is required.
48+
* @param dateFormatter the date formatter
49+
*/
50+
public void setFormatter(DateFormatter dateFormatter) {
51+
Assert.notNull(dateFormatter, "DateFormatter must not be null");
52+
this.dateFormatter = dateFormatter;
53+
}
54+
55+
4656
public void registerFormatters(FormatterRegistry registry) {
4757
addDateConverters(registry);
4858
registry.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory());
@@ -55,16 +65,6 @@ public void registerFormatters(FormatterRegistry registry) {
5565
}
5666
}
5767

58-
/**
59-
* Set the date formatter to register. If not specified no formatter is registered.
60-
* This method can be used if global formatter configuration is required.
61-
* @param dateFormatter the date formatter
62-
*/
63-
public void setFormatter(DateFormatter dateFormatter) {
64-
Assert.notNull(dateFormatter, "DateFormatter must not be null");
65-
this.dateFormatter = dateFormatter;
66-
}
67-
6868
/**
6969
* Add date converters to the specified registry.
7070
* @param converterRegistry the registry of converters to add to
@@ -80,13 +80,15 @@ public static void addDateConverters(ConverterRegistry converterRegistry) {
8080

8181

8282
private static class DateToLongConverter implements Converter<Date, Long> {
83+
8384
public Long convert(Date source) {
8485
return source.getTime();
8586
}
8687
}
8788

8889

8990
private static class DateToCalendarConverter implements Converter<Date, Calendar> {
91+
9092
public Calendar convert(Date source) {
9193
Calendar calendar = Calendar.getInstance();
9294
calendar.setTime(source);
@@ -96,20 +98,23 @@ public Calendar convert(Date source) {
9698

9799

98100
private static class CalendarToDateConverter implements Converter<Calendar, Date> {
101+
99102
public Date convert(Calendar source) {
100103
return source.getTime();
101104
}
102105
}
103106

104107

105108
private static class CalendarToLongConverter implements Converter<Calendar, Long> {
109+
106110
public Long convert(Calendar source) {
107111
return source.getTime().getTime();
108112
}
109113
}
110114

111115

112116
private static class LongToDateConverter implements Converter<Long, Date> {
117+
113118
public Date convert(Long source) {
114119
return new Date(source);
115120
}
@@ -118,10 +123,11 @@ public Date convert(Long source) {
118123

119124
private static class LongToCalendarConverter implements Converter<Long, Calendar> {
120125

121-
private DateToCalendarConverter dateToCalendarConverter = new DateToCalendarConverter();
126+
private final DateToCalendarConverter dateToCalendarConverter = new DateToCalendarConverter();
122127

123128
public Calendar convert(Long source) {
124129
return this.dateToCalendarConverter.convert(new Date(source));
125130
}
126131
}
132+
127133
}

‎spring-context/src/main/java/org/springframework/format/datetime/DateTimeFormatAnnotationFormatterFactory.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,7 +44,7 @@ public class DateTimeFormatAnnotationFormatterFactory implements
4444

4545
private static final Set<Class<?>> FIELD_TYPES;
4646
static {
47-
Set<Class<?>> fieldTypes = new HashSet<Class<?>>();
47+
Set<Class<?>> fieldTypes = new HashSet<Class<?>>(4);
4848
fieldTypes.add(Date.class);
4949
fieldTypes.add(Calendar.class);
5050
fieldTypes.add(Long.class);
@@ -82,4 +82,5 @@ protected Formatter<Date> getFormatter(DateTimeFormat annotation, Class<?> field
8282
protected String resolveEmbeddedValue(String value) {
8383
return (this.embeddedValueResolver != null ? this.embeddedValueResolver.resolveStringValue(value) : value);
8484
}
85+
8586
}

0 commit comments

Comments
 (0)