Skip to content

Commit 48b9e9a

Browse files
committed
Spring uses Joda-Time 2.0's specific parseLocalDate/parseLocalTime/parseLocalDateTime methods now
Issue: SPR-11014
1 parent 4bcfbc3 commit 48b9e9a

File tree

7 files changed

+184
-37
lines changed

7 files changed

+184
-37
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,10 +355,10 @@ project("spring-context") {
355355
optional("javax.enterprise.concurrent:javax.enterprise.concurrent-api:1.0")
356356
optional("org.apache.geronimo.specs:geronimo-jms_1.1_spec:1.1")
357357
optional("org.eclipse.persistence:javax.persistence:2.0.0")
358+
optional("joda-time:joda-time:2.2")
358359
optional("org.beanshell:bsh:2.0b4")
359360
optional("org.codehaus.groovy:groovy-all:${groovyVersion}")
360361
optional("org.jruby:jruby:1.7.2")
361-
optional("joda-time:joda-time:2.2")
362362
optional("org.slf4j:slf4j-api:${slf4jVersion}")
363363
optional("javax.validation:validation-api:1.0.0.GA")
364364
optional("org.hibernate:hibernate-validator:4.3.0.Final")

spring-context/src/main/java/org/springframework/format/datetime/joda/JodaDateTimeFormatAnnotationFormatterFactory.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.HashSet;
2323
import java.util.Set;
2424

25-
import org.joda.time.DateTime;
2625
import org.joda.time.LocalDate;
2726
import org.joda.time.LocalDateTime;
2827
import org.joda.time.LocalTime;
@@ -40,6 +39,8 @@
4039
/**
4140
* Formats fields annotated with the {@link DateTimeFormat} annotation using Joda-Time.
4241
*
42+
* <p><b>NOTE:</b> Spring's Joda-Time support requires Joda-Time 2.x, as of Spring 4.0.
43+
*
4344
* @author Keith Donald
4445
* @author Juergen Hoeller
4546
* @since 3.0
@@ -103,8 +104,19 @@ else if (ReadableInstant.class.isAssignableFrom(fieldType) || Calendar.class.isA
103104
}
104105

105106
@Override
106-
public Parser<DateTime> getParser(DateTimeFormat annotation, Class<?> fieldType) {
107-
return new DateTimeParser(getFormatter(annotation, fieldType));
107+
public Parser<?> getParser(DateTimeFormat annotation, Class<?> fieldType) {
108+
if (LocalDate.class.equals(fieldType)) {
109+
return new LocalDateParser(getFormatter(annotation, fieldType));
110+
}
111+
else if (LocalTime.class.equals(fieldType)) {
112+
return new LocalTimeParser(getFormatter(annotation, fieldType));
113+
}
114+
else if (LocalDateTime.class.equals(fieldType)) {
115+
return new LocalDateTimeParser(getFormatter(annotation, fieldType));
116+
}
117+
else {
118+
return new DateTimeParser(getFormatter(annotation, fieldType));
119+
}
108120
}
109121

110122
/**

spring-context/src/main/java/org/springframework/format/datetime/joda/JodaTimeFormatterRegistrar.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
/**
3939
* Configures Joda-Time's formatting system for use with Spring.
4040
*
41+
* <p><b>NOTE:</b> Spring's Joda-Time support requires Joda-Time 2.x, as of Spring 4.0.
42+
*
4143
* @author Keith Donald
4244
* @author Juergen Hoeller
4345
* @author Phillip Webb
@@ -165,17 +167,17 @@ public void registerFormatters(FormatterRegistry registry) {
165167

166168
addFormatterForFields(registry,
167169
new ReadablePartialPrinter(dateFormatter),
168-
new DateTimeParser(dateFormatter),
170+
new LocalDateParser(dateFormatter),
169171
LocalDate.class);
170172

171173
addFormatterForFields(registry,
172174
new ReadablePartialPrinter(timeFormatter),
173-
new DateTimeParser(timeFormatter),
175+
new LocalTimeParser(timeFormatter),
174176
LocalTime.class);
175177

176178
addFormatterForFields(registry,
177179
new ReadablePartialPrinter(dateTimeFormatter),
178-
new DateTimeParser(dateTimeFormatter),
180+
new LocalDateTimeParser(dateTimeFormatter),
179181
LocalDateTime.class);
180182

181183
addFormatterForFields(registry,
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2002-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.format.datetime.joda;
18+
19+
import java.text.ParseException;
20+
import java.util.Locale;
21+
22+
import org.joda.time.LocalDate;
23+
import org.joda.time.format.DateTimeFormatter;
24+
25+
import org.springframework.format.Parser;
26+
27+
/**
28+
* Parses Joda {@link org.joda.time.LocalDate} instances using a {@link org.joda.time.format.DateTimeFormatter}.
29+
*
30+
* @author Juergen Hoeller
31+
* @since 4.0
32+
*/
33+
public final class LocalDateParser implements Parser<LocalDate> {
34+
35+
private final DateTimeFormatter formatter;
36+
37+
38+
/**
39+
* Create a new DateTimeParser.
40+
* @param formatter the Joda DateTimeFormatter instance
41+
*/
42+
public LocalDateParser(DateTimeFormatter formatter) {
43+
this.formatter = formatter;
44+
}
45+
46+
47+
@Override
48+
public LocalDate parse(String text, Locale locale) throws ParseException {
49+
return JodaTimeContextHolder.getFormatter(this.formatter, locale).parseLocalDate(text);
50+
}
51+
52+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2002-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.format.datetime.joda;
18+
19+
import java.text.ParseException;
20+
import java.util.Locale;
21+
22+
import org.joda.time.LocalDateTime;
23+
import org.joda.time.format.DateTimeFormatter;
24+
25+
import org.springframework.format.Parser;
26+
27+
/**
28+
* Parses Joda {@link org.joda.time.LocalDateTime} instances using a {@link org.joda.time.format.DateTimeFormatter}.
29+
*
30+
* @author Juergen Hoeller
31+
* @since 4.0
32+
*/
33+
public final class LocalDateTimeParser implements Parser<LocalDateTime> {
34+
35+
private final DateTimeFormatter formatter;
36+
37+
38+
/**
39+
* Create a new DateTimeParser.
40+
* @param formatter the Joda DateTimeFormatter instance
41+
*/
42+
public LocalDateTimeParser(DateTimeFormatter formatter) {
43+
this.formatter = formatter;
44+
}
45+
46+
47+
@Override
48+
public LocalDateTime parse(String text, Locale locale) throws ParseException {
49+
return JodaTimeContextHolder.getFormatter(this.formatter, locale).parseLocalDateTime(text);
50+
}
51+
52+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2002-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.format.datetime.joda;
18+
19+
import java.text.ParseException;
20+
import java.util.Locale;
21+
22+
import org.joda.time.LocalTime;
23+
import org.joda.time.format.DateTimeFormatter;
24+
25+
import org.springframework.format.Parser;
26+
27+
/**
28+
* Parses Joda {@link org.joda.time.LocalTime} instances using a {@link org.joda.time.format.DateTimeFormatter}.
29+
*
30+
* @author Juergen Hoeller
31+
* @since 4.0
32+
*/
33+
public final class LocalTimeParser implements Parser<LocalTime> {
34+
35+
private final DateTimeFormatter formatter;
36+
37+
38+
/**
39+
* Create a new DateTimeParser.
40+
* @param formatter the Joda DateTimeFormatter instance
41+
*/
42+
public LocalTimeParser(DateTimeFormatter formatter) {
43+
this.formatter = formatter;
44+
}
45+
46+
47+
@Override
48+
public LocalTime parse(String text, Locale locale) throws ParseException {
49+
return JodaTimeContextHolder.getFormatter(this.formatter, locale).parseLocalTime(text);
50+
}
51+
52+
}

spring-context/src/test/java/org/springframework/format/datetime/joda/JodaTimeFormattingTests.java

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616

1717
package org.springframework.format.datetime.joda;
1818

19-
import static org.hamcrest.Matchers.equalTo;
20-
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.assertNotNull;
22-
import static org.junit.Assert.assertThat;
23-
2419
import java.util.ArrayList;
2520
import java.util.Calendar;
2621
import java.util.Date;
@@ -37,6 +32,7 @@
3732
import org.junit.After;
3833
import org.junit.Before;
3934
import org.junit.Test;
35+
4036
import org.springframework.beans.MutablePropertyValues;
4137
import org.springframework.context.i18n.LocaleContextHolder;
4238
import org.springframework.core.convert.TypeDescriptor;
@@ -46,6 +42,9 @@
4642
import org.springframework.format.support.FormattingConversionService;
4743
import org.springframework.validation.DataBinder;
4844

45+
import static org.hamcrest.Matchers.*;
46+
import static org.junit.Assert.*;
47+
4948
/**
5049
* @author Keith Donald
5150
* @author Juergen Hoeller
@@ -236,21 +235,10 @@ public void testBindLocalDateTime() {
236235
@Test
237236
public void testBindLocalDateTimeAnnotated() {
238237
MutablePropertyValues propertyValues = new MutablePropertyValues();
239-
propertyValues.add("localDateTimeAnnotated", "Saturday, October 31, 2009 12:00:00 PM ");
238+
propertyValues.add("localDateTimeAnnotated", "Oct 31, 2009 12:00 PM");
240239
binder.bind(propertyValues);
241240
assertEquals(0, binder.getBindingResult().getErrorCount());
242-
assertEquals("Saturday, October 31, 2009 12:00:00 PM ", binder.getBindingResult().getFieldValue(
243-
"localDateTimeAnnotated"));
244-
}
245-
246-
@Test
247-
public void testBindLocalDateTimeAnnotatedLong() {
248-
MutablePropertyValues propertyValues = new MutablePropertyValues();
249-
propertyValues.add("localDateTimeAnnotatedLong", "October 31, 2009 12:00:00 PM ");
250-
binder.bind(propertyValues);
251-
assertEquals(0, binder.getBindingResult().getErrorCount());
252-
assertEquals("October 31, 2009 12:00:00 PM ", binder.getBindingResult().getFieldValue(
253-
"localDateTimeAnnotatedLong"));
241+
assertEquals("Oct 31, 2009 12:00 PM", binder.getBindingResult().getFieldValue("localDateTimeAnnotated"));
254242
}
255243

256244
@Test
@@ -510,12 +498,9 @@ private static class JodaTimeBean {
510498

511499
private LocalDateTime localDateTime;
512500

513-
@DateTimeFormat(style="FF")
501+
@DateTimeFormat(style="MS")
514502
private LocalDateTime localDateTimeAnnotated;
515503

516-
@DateTimeFormat(style="LL")
517-
private LocalDateTime localDateTimeAnnotatedLong;
518-
519504
private DateTime dateTime;
520505

521506
@DateTimeFormat(style="MS")
@@ -612,14 +597,6 @@ public void setLocalDateTimeAnnotated(LocalDateTime localDateTimeAnnotated) {
612597
this.localDateTimeAnnotated = localDateTimeAnnotated;
613598
}
614599

615-
public LocalDateTime getLocalDateTimeAnnotatedLong() {
616-
return localDateTimeAnnotatedLong;
617-
}
618-
619-
public void setLocalDateTimeAnnotatedLong(LocalDateTime localDateTimeAnnotatedLong) {
620-
this.localDateTimeAnnotatedLong = localDateTimeAnnotatedLong;
621-
}
622-
623600
public DateTime getDateTime() {
624601
return dateTime;
625602
}

0 commit comments

Comments
 (0)