From 381d1d5a94ae03e1a5dcec4a8e71e8c41d54f1bf Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Sun, 25 Oct 2009 04:52:26 +0000 Subject: [PATCH] default date/time converters --- org.springframework.context/ivy.xml | 2 +- org.springframework.core/.classpath | 1 + org.springframework.core/ivy.xml | 1 + org.springframework.core/pom.xml | 6 + .../support/CalendarToDateConverter.java | 33 +++++ .../support/DateToCalendarConverter.java | 35 +++++ .../support/DefaultConversionService.java | 5 + .../convert/support/JodaTimeConverters.java | 130 ++++++++++++++++++ org.springframework.core/template.mf | 1 + 9 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/support/CalendarToDateConverter.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/support/DateToCalendarConverter.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/support/JodaTimeConverters.java diff --git a/org.springframework.context/ivy.xml b/org.springframework.context/ivy.xml index b5f2837754d..b531ca58618 100644 --- a/org.springframework.context/ivy.xml +++ b/org.springframework.context/ivy.xml @@ -46,7 +46,7 @@ - + diff --git a/org.springframework.core/.classpath b/org.springframework.core/.classpath index d4508b4e23f..6f73abf26f1 100644 --- a/org.springframework.core/.classpath +++ b/org.springframework.core/.classpath @@ -15,5 +15,6 @@ + diff --git a/org.springframework.core/ivy.xml b/org.springframework.core/ivy.xml index 48cf8f9a438..32c6e18edde 100644 --- a/org.springframework.core/ivy.xml +++ b/org.springframework.core/ivy.xml @@ -26,6 +26,7 @@ + diff --git a/org.springframework.core/pom.xml b/org.springframework.core/pom.xml index ad0aaa9dc85..bd6948545f8 100644 --- a/org.springframework.core/pom.xml +++ b/org.springframework.core/pom.xml @@ -41,6 +41,12 @@ aspectjweaver true + + joda-time + joda-time + 1.6 + true + org.jboss.vfs com.springsource.org.jboss.virtual diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CalendarToDateConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CalendarToDateConverter.java new file mode 100644 index 00000000000..bb41a89ad13 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CalendarToDateConverter.java @@ -0,0 +1,33 @@ +/* + * Copyright 2002-2009 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.core.convert.support; + +import java.util.Calendar; +import java.util.Date; + +import org.springframework.core.convert.converter.Converter; + +/** + * Converts from a java.util.Calendar to a java.util.Date. + * @author Keith Donald + */ +final class CalendarToDateConverter implements Converter { + + public Date convert(Calendar source) { + return source.getTime(); + } + +} diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/DateToCalendarConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/DateToCalendarConverter.java new file mode 100644 index 00000000000..c0538910a03 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/DateToCalendarConverter.java @@ -0,0 +1,35 @@ +/* + * Copyright 2002-2009 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.core.convert.support; + +import java.util.Calendar; +import java.util.Date; + +import org.springframework.core.convert.converter.Converter; + +/** + * Converts from a java.util.Date to a java.util.Calendar. + * @author Keith Donald + */ +final class DateToCalendarConverter implements Converter { + + public Calendar convert(Date source) { + Calendar cal = Calendar.getInstance(); + cal.setTime(source); + return cal; + } + +} diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java index dba0a1300a4..81697b66ec9 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java @@ -16,6 +16,8 @@ package org.springframework.core.convert.support; +import java.util.Calendar; +import java.util.Date; import java.util.Locale; /** @@ -36,6 +38,9 @@ public class DefaultConversionService extends GenericConversionService { addConverter(String.class, Character.class, new StringToCharacterConverter()); addConverter(String.class, Locale.class, new StringToLocaleConverter()); addConverter(Number.class, Character.class, new NumberToCharacterConverter()); + addConverter(Date.class, Calendar.class, new DateToCalendarConverter()); + addConverter(Calendar.class, Date.class, new CalendarToDateConverter()); + JodaTimeConverters.addConverters(this); addConverter(Object.class, String.class, new ObjectToStringConverter()); addConverterFactory(String.class, Number.class, new StringToNumberConverterFactory()); addConverterFactory(String.class, Enum.class, new StringToEnumConverterFactory()); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/JodaTimeConverters.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/JodaTimeConverters.java new file mode 100644 index 00000000000..6976cb29eae --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/JodaTimeConverters.java @@ -0,0 +1,130 @@ +/* + * Copyright 2002-2009 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.core.convert.support; + +import java.util.Calendar; +import java.util.Date; + +import org.joda.time.DateMidnight; +import org.joda.time.DateTime; +import org.joda.time.LocalDate; +import org.joda.time.LocalDateTime; +import org.joda.time.LocalTime; +import org.springframework.core.convert.converter.Converter; +import org.springframework.util.ClassUtils; + +class JodaTimeConverters { + + public static void addConverters(GenericConversionService registry) { + if (!isJodaTimePresent()) { + return; + } + registry.addConverter(DateTime.class, LocalDate.class, new DateTimeToLocalDateConverter()); + registry.addConverter(LocalDate.class, DateTime.class, new LocalDateToDateTimeConverter()); + + registry.addConverter(DateTime.class, LocalTime.class, new DateTimeToLocalTimeConverter()); + registry.addConverter(LocalTime.class, DateTime.class, new LocalTimeToDateTimeConverter()); + + registry.addConverter(DateTime.class, LocalDateTime.class, new DateTimeToLocalDateTimeConverter()); + registry.addConverter(LocalDateTime.class, DateTime.class, new LocalDateTimeToDateTimeConverter()); + + registry.addConverter(DateTime.class, DateMidnight.class, new DateTimeToDateMidnightConverter()); + registry.addConverter(DateMidnight.class, Date.class, new DateMidnightToDateTimeConverter()); + + registry.addConverter(DateTime.class, Date.class, new DateTimeToDateConverter()); + registry.addConverter(Date.class, DateTime.class, new DateToDateTimeConverter()); + + registry.addConverter(DateTime.class, Calendar.class, new DateTimeToCalendarConverter()); + registry.addConverter(Calendar.class, DateTime.class, new CalendarToDateTimeConverter()); + } + + private static boolean isJodaTimePresent() { + return ClassUtils.isPresent("org.joda.time.DateTime", JodaTimeConverters.class.getClassLoader()); + } + + private static class DateTimeToLocalDateConverter implements Converter { + public LocalDate convert(DateTime source) { + return source.toLocalDate(); + } + } + + private static class LocalDateToDateTimeConverter implements Converter { + public DateTime convert(LocalDate source) { + return source.toDateTimeAtStartOfDay(); + } + } + + private static class DateTimeToLocalTimeConverter implements Converter { + public LocalTime convert(DateTime source) { + return source.toLocalTime(); + } + } + + private static class LocalTimeToDateTimeConverter implements Converter { + public DateTime convert(LocalTime source) { + return source.toDateTimeToday(); + } + } + + private static class DateTimeToLocalDateTimeConverter implements Converter { + public LocalDateTime convert(DateTime source) { + return source.toLocalDateTime(); + } + } + + private static class LocalDateTimeToDateTimeConverter implements Converter { + public DateTime convert(LocalDateTime source) { + return source.toDateTime(); + } + } + + private static class DateTimeToDateMidnightConverter implements Converter { + public DateMidnight convert(DateTime source) { + return source.toDateMidnight(); + } + } + + private static class DateMidnightToDateTimeConverter implements Converter { + public DateTime convert(DateMidnight source) { + return source.toDateTime(); + } + } + + private static class DateTimeToDateConverter implements Converter { + public Date convert(DateTime source) { + return source.toDate(); + } + } + + private static class DateToDateTimeConverter implements Converter { + public DateTime convert(Date source) { + return new DateTime(source); + } + } + + private static class DateTimeToCalendarConverter implements Converter { + public Calendar convert(DateTime source) { + return source.toGregorianCalendar(); + } + } + + private static class CalendarToDateTimeConverter implements Converter { + public DateTime convert(Calendar source) { + return new DateTime(source); + } + } + +} diff --git a/org.springframework.core/template.mf b/org.springframework.core/template.mf index 218e9ab926c..23560197576 100644 --- a/org.springframework.core/template.mf +++ b/org.springframework.core/template.mf @@ -10,6 +10,7 @@ Import-Template: org.springframework.asm.*;version="[3.0.0, 3.0.1)";resolution:=optional, org.apache.log4j.*;version="[1.2.15, 2.0.0)";resolution:=optional, org.aspectj.*;version="[1.5.4, 2.0.0)";resolution:=optional, + org.joda.*;version="[1.6.0, 2.0.0)";resolution:=optional, org.jboss.virtual.*;version="[2.1.0.GA, 3.0.0)";resolution:=optional, org.xml.sax.*;version="0";resolution:=optional, org.w3c.dom.*;version="0";resolution:=optional