Skip to content

Commit 381d1d5

Browse files
author
Keith Donald
committed
default date/time converters
1 parent d868785 commit 381d1d5

File tree

9 files changed

+213
-1
lines changed

9 files changed

+213
-1
lines changed

org.springframework.context/ivy.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
<dependency org="org.aspectj" name="com.springsource.org.aspectj.weaver" rev="1.6.5.RELEASE" conf="optional, aspectj->compile"/>
4747
<dependency org="org.beanshell" name="com.springsource.bsh" rev="2.0.0.b4" conf="optional, beanshell->compile"/>
4848
<dependency org="org.codehaus.groovy" name="com.springsource.org.codehaus.groovy" rev="1.6.3" conf="optional, groovy->compile"/>
49-
<dependency org="org.joda" name="com.springsource.org.joda.time" rev="1.6.0" />
49+
<dependency org="org.joda" name="com.springsource.org.joda.time" rev="1.6.0" conf="optional->compile"/>
5050
<dependency org="org.jruby" name="com.springsource.org.jruby" rev="1.2.0" conf="optional, jruby->compile"/>
5151
<dependency org="org.springframework" name="org.springframework.asm" rev="latest.integration" conf="compile->compile"/>
5252
<dependency org="org.springframework" name="org.springframework.aop" rev="latest.integration" conf="compile->compile"/>

org.springframework.core/.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
<classpathentry kind="var" path="IVY_CACHE/org.codehaus.woodstox/com.springsource.com.ctc.wstx/3.2.7/com.springsource.com.ctc.wstx-3.2.7.jar" sourcepath="/IVY_CACHE/org.codehaus.woodstox/com.springsource.com.ctc.wstx/3.2.7/com.springsource.com.ctc.wstx-sources-3.2.7.jar"/>
1616
<classpathentry kind="lib" path="/org.springframework.asm/target/artifacts/org.springframework.asm.jar" sourcepath="/org.springframework.asm/target/artifacts/org.springframework.asm-sources.jar"/>
1717
<classpathentry kind="var" path="IVY_CACHE/org.jboss.vfs/com.springsource.org.jboss.virtual/2.1.0.GA/com.springsource.org.jboss.virtual-2.1.0.GA.jar"/>
18+
<classpathentry kind="var" path="IVY_CACHE/org.joda/com.springsource.org.joda.time/1.6.0/com.springsource.org.joda.time-1.6.0.jar" sourcepath="/IVY_CACHE/org.joda/com.springsource.org.joda.time/1.6.0/com.springsource.org.joda.time-sources-1.6.0.jar"/>
1819
<classpathentry kind="output" path="target/classes"/>
1920
</classpath>

org.springframework.core/ivy.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<dependency org="org.apache.log4j" name="com.springsource.org.apache.log4j" rev="1.2.15" conf="optional, log4j->compile"/>
2727
<dependency org="org.aspectj" name="com.springsource.org.aspectj.weaver" rev="1.6.5.RELEASE" conf="optional, aspectj->compile"/>
2828
<dependency org="org.jboss.vfs" name="com.springsource.org.jboss.virtual" rev="2.1.0.GA" conf="optional->compile"/>
29+
<dependency org="org.joda" name="com.springsource.org.joda.time" rev="1.6.0" conf="optional->compile"/>
2930
<dependency org="org.springframework" name="org.springframework.asm" rev="latest.integration" conf="optional->compile"/>
3031
<!-- test dependencies -->
3132
<dependency org="javax.servlet" name="com.springsource.javax.servlet" rev="2.5.0" conf="test->compile"/>

org.springframework.core/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141
<artifactId>aspectjweaver</artifactId>
4242
<optional>true</optional>
4343
</dependency>
44+
<dependency>
45+
<groupId>joda-time</groupId>
46+
<artifactId>joda-time</artifactId>
47+
<version>1.6</version>
48+
<optional>true</optional>
49+
</dependency>
4450
<dependency>
4551
<groupId>org.jboss.vfs</groupId>
4652
<artifactId>com.springsource.org.jboss.virtual</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2002-2009 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+
package org.springframework.core.convert.support;
17+
18+
import java.util.Calendar;
19+
import java.util.Date;
20+
21+
import org.springframework.core.convert.converter.Converter;
22+
23+
/**
24+
* Converts from a java.util.Calendar to a java.util.Date.
25+
* @author Keith Donald
26+
*/
27+
final class CalendarToDateConverter implements Converter<Calendar, Date> {
28+
29+
public Date convert(Calendar source) {
30+
return source.getTime();
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2002-2009 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+
package org.springframework.core.convert.support;
17+
18+
import java.util.Calendar;
19+
import java.util.Date;
20+
21+
import org.springframework.core.convert.converter.Converter;
22+
23+
/**
24+
* Converts from a java.util.Date to a java.util.Calendar.
25+
* @author Keith Donald
26+
*/
27+
final class DateToCalendarConverter implements Converter<Date, Calendar> {
28+
29+
public Calendar convert(Date source) {
30+
Calendar cal = Calendar.getInstance();
31+
cal.setTime(source);
32+
return cal;
33+
}
34+
35+
}

org.springframework.core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.core.convert.support;
1818

19+
import java.util.Calendar;
20+
import java.util.Date;
1921
import java.util.Locale;
2022

2123
/**
@@ -36,6 +38,9 @@ public DefaultConversionService() {
3638
addConverter(String.class, Character.class, new StringToCharacterConverter());
3739
addConverter(String.class, Locale.class, new StringToLocaleConverter());
3840
addConverter(Number.class, Character.class, new NumberToCharacterConverter());
41+
addConverter(Date.class, Calendar.class, new DateToCalendarConverter());
42+
addConverter(Calendar.class, Date.class, new CalendarToDateConverter());
43+
JodaTimeConverters.addConverters(this);
3944
addConverter(Object.class, String.class, new ObjectToStringConverter());
4045
addConverterFactory(String.class, Number.class, new StringToNumberConverterFactory());
4146
addConverterFactory(String.class, Enum.class, new StringToEnumConverterFactory());
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Copyright 2002-2009 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+
package org.springframework.core.convert.support;
17+
18+
import java.util.Calendar;
19+
import java.util.Date;
20+
21+
import org.joda.time.DateMidnight;
22+
import org.joda.time.DateTime;
23+
import org.joda.time.LocalDate;
24+
import org.joda.time.LocalDateTime;
25+
import org.joda.time.LocalTime;
26+
import org.springframework.core.convert.converter.Converter;
27+
import org.springframework.util.ClassUtils;
28+
29+
class JodaTimeConverters {
30+
31+
public static void addConverters(GenericConversionService registry) {
32+
if (!isJodaTimePresent()) {
33+
return;
34+
}
35+
registry.addConverter(DateTime.class, LocalDate.class, new DateTimeToLocalDateConverter());
36+
registry.addConverter(LocalDate.class, DateTime.class, new LocalDateToDateTimeConverter());
37+
38+
registry.addConverter(DateTime.class, LocalTime.class, new DateTimeToLocalTimeConverter());
39+
registry.addConverter(LocalTime.class, DateTime.class, new LocalTimeToDateTimeConverter());
40+
41+
registry.addConverter(DateTime.class, LocalDateTime.class, new DateTimeToLocalDateTimeConverter());
42+
registry.addConverter(LocalDateTime.class, DateTime.class, new LocalDateTimeToDateTimeConverter());
43+
44+
registry.addConverter(DateTime.class, DateMidnight.class, new DateTimeToDateMidnightConverter());
45+
registry.addConverter(DateMidnight.class, Date.class, new DateMidnightToDateTimeConverter());
46+
47+
registry.addConverter(DateTime.class, Date.class, new DateTimeToDateConverter());
48+
registry.addConverter(Date.class, DateTime.class, new DateToDateTimeConverter());
49+
50+
registry.addConverter(DateTime.class, Calendar.class, new DateTimeToCalendarConverter());
51+
registry.addConverter(Calendar.class, DateTime.class, new CalendarToDateTimeConverter());
52+
}
53+
54+
private static boolean isJodaTimePresent() {
55+
return ClassUtils.isPresent("org.joda.time.DateTime", JodaTimeConverters.class.getClassLoader());
56+
}
57+
58+
private static class DateTimeToLocalDateConverter implements Converter<DateTime, LocalDate> {
59+
public LocalDate convert(DateTime source) {
60+
return source.toLocalDate();
61+
}
62+
}
63+
64+
private static class LocalDateToDateTimeConverter implements Converter<LocalDate, DateTime> {
65+
public DateTime convert(LocalDate source) {
66+
return source.toDateTimeAtStartOfDay();
67+
}
68+
}
69+
70+
private static class DateTimeToLocalTimeConverter implements Converter<DateTime, LocalTime> {
71+
public LocalTime convert(DateTime source) {
72+
return source.toLocalTime();
73+
}
74+
}
75+
76+
private static class LocalTimeToDateTimeConverter implements Converter<LocalTime, DateTime> {
77+
public DateTime convert(LocalTime source) {
78+
return source.toDateTimeToday();
79+
}
80+
}
81+
82+
private static class DateTimeToLocalDateTimeConverter implements Converter<DateTime, LocalDateTime> {
83+
public LocalDateTime convert(DateTime source) {
84+
return source.toLocalDateTime();
85+
}
86+
}
87+
88+
private static class LocalDateTimeToDateTimeConverter implements Converter<LocalDateTime, DateTime> {
89+
public DateTime convert(LocalDateTime source) {
90+
return source.toDateTime();
91+
}
92+
}
93+
94+
private static class DateTimeToDateMidnightConverter implements Converter<DateTime, DateMidnight> {
95+
public DateMidnight convert(DateTime source) {
96+
return source.toDateMidnight();
97+
}
98+
}
99+
100+
private static class DateMidnightToDateTimeConverter implements Converter<DateMidnight, DateTime> {
101+
public DateTime convert(DateMidnight source) {
102+
return source.toDateTime();
103+
}
104+
}
105+
106+
private static class DateTimeToDateConverter implements Converter<DateTime, Date> {
107+
public Date convert(DateTime source) {
108+
return source.toDate();
109+
}
110+
}
111+
112+
private static class DateToDateTimeConverter implements Converter<Date, DateTime> {
113+
public DateTime convert(Date source) {
114+
return new DateTime(source);
115+
}
116+
}
117+
118+
private static class DateTimeToCalendarConverter implements Converter<DateTime, Calendar> {
119+
public Calendar convert(DateTime source) {
120+
return source.toGregorianCalendar();
121+
}
122+
}
123+
124+
private static class CalendarToDateTimeConverter implements Converter<Calendar, DateTime> {
125+
public DateTime convert(Calendar source) {
126+
return new DateTime(source);
127+
}
128+
}
129+
130+
}

org.springframework.core/template.mf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Import-Template:
1010
org.springframework.asm.*;version="[3.0.0, 3.0.1)";resolution:=optional,
1111
org.apache.log4j.*;version="[1.2.15, 2.0.0)";resolution:=optional,
1212
org.aspectj.*;version="[1.5.4, 2.0.0)";resolution:=optional,
13+
org.joda.*;version="[1.6.0, 2.0.0)";resolution:=optional,
1314
org.jboss.virtual.*;version="[2.1.0.GA, 3.0.0)";resolution:=optional,
1415
org.xml.sax.*;version="0";resolution:=optional,
1516
org.w3c.dom.*;version="0";resolution:=optional

0 commit comments

Comments
 (0)