Author: adrianc
Date: Sun Apr 11 08:08:28 2010
New Revision: 932851

URL: http://svn.apache.org/viewvc?rev=932851&view=rev
Log:
Improved date/time conversion reflection, added date/time converter localized 
tests.

Modified:
    
commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/DateTimeConverters.java
    
commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestDateTimeConverters.java

Modified: 
commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/DateTimeConverters.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/DateTimeConverters.java?rev=932851&r1=932850&r2=932851&view=diff
==============================================================================
--- 
commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/DateTimeConverters.java
 (original)
+++ 
commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/DateTimeConverters.java
 Sun Apr 11 08:08:28 2010
@@ -18,6 +18,7 @@
  
*******************************************************************************/
 package org.apache.commons.convert;
 
+import java.sql.Time;
 import java.sql.Timestamp;
 import java.text.DateFormat;
 import java.text.ParseException;
@@ -30,37 +31,34 @@ import java.util.TimeZone;
 /** Date/time Converter classes. */
 public class DateTimeConverters implements ConverterLoader {
 
-    public static final String CALENDAR_FORMAT = "EEE MMM dd HH:mm:ss.SSS zzz 
yyyy";
+    public static final String CALENDAR_FORMAT = "EEE MMM dd HH:mm:ss.SSS z 
yyyy";
+    public static final String JDBC_DATE_FORMAT = "yyyy-MM-dd";
+    public static final String JDBC_TIME_FORMAT = "HH:mm:ss";
+    public static final String JDBC_TIMESTAMP_FORMAT = "yyyy-MM-dd 
HH:mm:ss.SSS";
 
     /**
      * Returns an initialized DateFormat object.
-     *
-     * @param dateFormat
-     *            optional format string
+     * 
      * @param tz
-     * @param locale
-     *            can be null if dateFormat is not null
      * @return DateFormat object
      */
-    public static DateFormat toDateFormat(String dateFormat, TimeZone tz, 
Locale locale) {
-        DateFormat df = null;
-        if (Util.isEmpty(dateFormat)) {
-            df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
-        } else {
-            df = new SimpleDateFormat(dateFormat);
-        }
+    protected static DateFormat toDateFormat(TimeZone tz) {
+        DateFormat df = new SimpleDateFormat(JDBC_DATE_FORMAT);
         df.setTimeZone(tz);
         return df;
     }
 
     /**
      * Returns an initialized DateFormat object.
-     * @param dateTimeFormat optional format string
+     * 
+     * @param dateTimeFormat
+     *            optional format string
      * @param tz
-     * @param locale can be null if dateTimeFormat is not null
+     * @param locale
+     *            can be null if dateTimeFormat is not null
      * @return DateFormat object
      */
-    public static DateFormat toDateTimeFormat(String dateTimeFormat, TimeZone 
tz, Locale locale) {
+    protected static DateFormat toDateTimeFormat(String dateTimeFormat, 
TimeZone tz, Locale locale) {
         DateFormat df = null;
         if (Util.isEmpty(dateTimeFormat)) {
             df = DateFormat.getDateTimeInstance(DateFormat.SHORT, 
DateFormat.MEDIUM, locale);
@@ -73,18 +71,12 @@ public class DateTimeConverters implemen
 
     /**
      * Returns an initialized DateFormat object.
-     * @param timeFormat optional format string
+     * 
      * @param tz
-     * @param locale can be null if timeFormat is not null
      * @return DateFormat object
      */
-    public static DateFormat toTimeFormat(String timeFormat, TimeZone tz, 
Locale locale) {
-        DateFormat df = null;
-        if (Util.isEmpty(timeFormat)) {
-            df = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
-        } else {
-            df = new SimpleDateFormat(timeFormat);
-        }
+    protected static DateFormat toTimeFormat(TimeZone tz) {
+        DateFormat df = new SimpleDateFormat(JDBC_TIME_FORMAT);
         df.setTimeZone(tz);
         return df;
     }
@@ -107,8 +99,6 @@ public class DateTimeConverters implemen
         Converters.registerConverter(new 
GenericSingletonToSet<java.sql.Date>(java.sql.Date.class));
         Converters.registerConverter(new 
GenericSingletonToSet<java.sql.Time>(java.sql.Time.class));
         Converters.registerConverter(new 
GenericSingletonToSet<java.sql.Timestamp>(java.sql.Timestamp.class));
-        Converters.registerConverter(new 
GenericToStringConverter<java.sql.Date>(java.sql.Date.class));
-        Converters.registerConverter(new 
GenericToStringConverter<java.sql.Time>(java.sql.Time.class));
     }
 
     public static class CalendarToLong extends AbstractConverter<Calendar, 
Long> {
@@ -134,7 +124,7 @@ public class DateTimeConverters implemen
         }
 
         public String convert(Calendar obj, Locale locale, TimeZone timeZone, 
String formatString) throws ConversionException {
-            DateFormat df = toDateTimeFormat(formatString, timeZone, locale);
+            DateFormat df = toDateTimeFormat(formatString == null ? 
CALENDAR_FORMAT : formatString, timeZone, locale);
             df.setCalendar(obj);
             return df.format(obj.getTime());
         }
@@ -167,7 +157,7 @@ public class DateTimeConverters implemen
         }
 
         public String convert(Date obj, Locale locale, TimeZone timeZone, 
String formatString) throws ConversionException {
-            DateFormat df = toDateTimeFormat(formatString, timeZone, locale);
+            DateFormat df = toDateTimeFormat(formatString == null ? 
CALENDAR_FORMAT : formatString, timeZone, locale);
             return df.format(obj);
         }
     }
@@ -193,7 +183,7 @@ public class DateTimeConverters implemen
         }
 
         public Long convert(S obj) throws ConversionException {
-             return obj.getTime();
+            return obj.getTime();
         }
     }
 
@@ -207,7 +197,7 @@ public class DateTimeConverters implemen
         }
     }
 
-    public static class LongToCalendar extends 
AbstractLocalizedConverter<Long, Calendar> {
+    public static class LongToCalendar extends GenericLocalizedConverter<Long, 
Calendar> {
         public LongToCalendar() {
             super(Long.class, Calendar.class);
         }
@@ -218,15 +208,11 @@ public class DateTimeConverters implemen
             return cal;
         }
 
-        public Calendar convert(Long obj, Locale locale, TimeZone timeZone) 
throws ConversionException {
+        public Calendar convert(Long obj, Locale locale, TimeZone timeZone, 
String formatString) throws ConversionException {
             Calendar cal = Calendar.getInstance(timeZone, locale);
             cal.setTimeInMillis(obj);
             return cal;
         }
-
-        public Calendar convert(Long obj, Locale locale, TimeZone timeZone, 
String formatString) throws ConversionException {
-            return convert(obj, locale, timeZone);
-        }
     }
 
     public static class LongToDate extends AbstractConverter<Long, Date> {
@@ -235,7 +221,7 @@ public class DateTimeConverters implemen
         }
 
         public Date convert(Long obj) throws ConversionException {
-             return new Date(obj.longValue());
+            return new Date(obj.longValue());
         }
     }
 
@@ -245,7 +231,7 @@ public class DateTimeConverters implemen
         }
 
         public java.sql.Date convert(Long obj) throws ConversionException {
-             return new java.sql.Date(obj.longValue());
+            return new java.sql.Date(obj.longValue());
         }
     }
 
@@ -255,7 +241,7 @@ public class DateTimeConverters implemen
         }
 
         public java.sql.Time convert(Long obj) throws ConversionException {
-             return new java.sql.Time(obj.longValue());
+            return new java.sql.Time(obj.longValue());
         }
     }
 
@@ -265,7 +251,7 @@ public class DateTimeConverters implemen
         }
 
         public java.sql.Timestamp convert(Long obj) throws ConversionException 
{
-             return new java.sql.Timestamp(obj.longValue());
+            return new java.sql.Timestamp(obj.longValue());
         }
     }
 
@@ -284,6 +270,22 @@ public class DateTimeConverters implemen
         }
     }
 
+    public static class SqlDateToString extends 
GenericLocalizedConverter<java.sql.Date, String> {
+        public SqlDateToString() {
+            super(java.sql.Date.class, String.class);
+        }
+
+        @Override
+        public String convert(java.sql.Date obj) throws ConversionException {
+            return obj.toString();
+        }
+
+        public String convert(java.sql.Date obj, Locale locale, TimeZone 
timeZone, String formatString) throws ConversionException {
+            DateFormat df = toDateFormat(timeZone);
+            return df.format(obj);
+        }
+    }
+
     public static class SqlDateToTimestamp extends 
AbstractConverter<java.sql.Date, java.sql.Timestamp> {
         public SqlDateToTimestamp() {
             super(java.sql.Date.class, java.sql.Timestamp.class);
@@ -296,7 +298,23 @@ public class DateTimeConverters implemen
 
         public java.sql.Timestamp convert(java.sql.Date obj) throws 
ConversionException {
             return new java.sql.Timestamp(obj.getTime());
-       }
+        }
+    }
+
+    public static class SqlTimeToString extends 
GenericLocalizedConverter<java.sql.Time, String> {
+        public SqlTimeToString() {
+            super(java.sql.Time.class, String.class);
+        }
+
+        @Override
+        public String convert(java.sql.Time obj) throws ConversionException {
+            return obj.toString();
+        }
+
+        public String convert(java.sql.Time obj, Locale locale, TimeZone 
timeZone, String formatString) throws ConversionException {
+            DateFormat df = toTimeFormat(timeZone);
+            return df.format(obj);
+        }
     }
 
     public static class StringToCalendar extends 
GenericLocalizedConverter<String, Calendar> {
@@ -316,7 +334,7 @@ public class DateTimeConverters implemen
         }
 
         public Calendar convert(String obj, Locale locale, TimeZone timeZone, 
String formatString) throws ConversionException {
-            DateFormat df = toDateTimeFormat(formatString, timeZone, locale);
+            DateFormat df = toDateTimeFormat(formatString == null ? 
CALENDAR_FORMAT : formatString, timeZone, locale);
             try {
                 Date date = df.parse(obj);
                 Calendar cal = Calendar.getInstance(timeZone, locale);
@@ -344,7 +362,7 @@ public class DateTimeConverters implemen
         }
 
         public Date convert(String obj, Locale locale, TimeZone timeZone, 
String formatString) throws ConversionException {
-            DateFormat df = toDateTimeFormat(formatString, timeZone, locale);
+            DateFormat df = toDateTimeFormat(formatString == null ? 
CALENDAR_FORMAT : formatString, timeZone, locale);
             try {
                 return df.parse(obj);
             } catch (ParseException e) {
@@ -353,7 +371,7 @@ public class DateTimeConverters implemen
         }
     }
 
-    public static class StringToSqlDate extends AbstractConverter<String, 
java.sql.Date> {
+    public static class StringToSqlDate extends 
GenericLocalizedConverter<String, java.sql.Date> {
         public StringToSqlDate() {
             super(String.class, java.sql.Date.class);
         }
@@ -362,9 +380,19 @@ public class DateTimeConverters implemen
         public java.sql.Date convert(String obj) throws ConversionException {
             return java.sql.Date.valueOf(obj);
         }
+
+        @Override
+        public java.sql.Date convert(String obj, Locale locale, TimeZone 
timeZone, String formatString) throws ConversionException {
+            DateFormat df = toDateFormat(timeZone);
+            try {
+                return new java.sql.Date(df.parse(obj).getTime());
+            } catch (ParseException e) {
+                throw new ConversionException(e);
+            }
+        }
     }
 
-    public static class StringToSqlTime extends AbstractConverter<String, 
java.sql.Time> {
+    public static class StringToSqlTime extends 
GenericLocalizedConverter<String, java.sql.Time> {
         public StringToSqlTime() {
             super(String.class, java.sql.Time.class);
         }
@@ -373,6 +401,16 @@ public class DateTimeConverters implemen
         public java.sql.Time convert(String obj) throws ConversionException {
             return java.sql.Time.valueOf(obj);
         }
+
+        @Override
+        public Time convert(String obj, Locale locale, TimeZone timeZone, 
String formatString) throws ConversionException {
+            DateFormat df = toTimeFormat(timeZone);
+            try {
+                return new java.sql.Time(df.parse(obj).getTime());
+            } catch (ParseException e) {
+                throw new ConversionException(e);
+            }
+        }
     }
 
     public static class StringToTimestamp extends 
GenericLocalizedConverter<String, java.sql.Timestamp> {
@@ -386,7 +424,7 @@ public class DateTimeConverters implemen
         }
 
         public java.sql.Timestamp convert(String obj, Locale locale, TimeZone 
timeZone, String formatString) throws ConversionException {
-            DateFormat df = toDateTimeFormat(formatString, timeZone, locale);
+            DateFormat df = toDateTimeFormat(formatString == null ? 
JDBC_TIMESTAMP_FORMAT : formatString, timeZone, locale);
             try {
                 return new java.sql.Timestamp(df.parse(obj).getTime());
             } catch (ParseException e) {
@@ -431,7 +469,7 @@ public class DateTimeConverters implemen
         }
 
         public String convert(java.sql.Timestamp obj, Locale locale, TimeZone 
timeZone, String formatString) throws ConversionException {
-            DateFormat df = toDateFormat(formatString, timeZone, locale);
+            DateFormat df = toDateTimeFormat(formatString == null ? 
JDBC_TIMESTAMP_FORMAT : formatString, timeZone, locale);
             return df.format(obj);
         }
     }

Modified: 
commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestDateTimeConverters.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestDateTimeConverters.java?rev=932851&r1=932850&r2=932851&view=diff
==============================================================================
--- 
commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestDateTimeConverters.java
 (original)
+++ 
commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestDateTimeConverters.java
 Sun Apr 11 08:08:28 2010
@@ -23,6 +23,7 @@ import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Collection;
 import java.util.List;
+import java.util.Locale;
 import java.util.Set;
 import java.util.TimeZone;
 
@@ -41,12 +42,23 @@ public class TestDateTimeConverters exte
             assertEquals(label + " registered", converter.getClass(), 
Converters.getConverter(source.getClass(), target.getClass()).getClass());
         }
         assertEquals(label + " converted", target, converter.convert(source));
+        Converter<T, S> reflectiveConverter = null;
         try {
-            Converter<T, S> reflectiveConverter = (Converter<T, S>) 
Converters.getConverter(target.getClass(), source.getClass());
+            reflectiveConverter = (Converter<T, S>) 
Converters.getConverter(target.getClass(), source.getClass());
             assertEquals(label + " reflection converted", source, 
reflectiveConverter.convert(target));
         } catch (ClassNotFoundException e) {
             System.out.println(converter.getClass() + " not reflective");
         }
+        try {
+            LocalizedConverter<S,T> localizedConverter = (LocalizedConverter) 
converter;
+            T localizedResult = localizedConverter.convert(source, 
Locale.getDefault(), TimeZone.getDefault());
+            T formattedResult = localizedConverter.convert(source, 
Locale.getDefault(), TimeZone.getDefault(), DateTimeConverters.CALENDAR_FORMAT);
+            if (reflectiveConverter != null) {
+                LocalizedConverter<T, S> localizedReflectiveConverter = 
(LocalizedConverter) reflectiveConverter;
+                assertEquals(label + " localized reflection converted", 
source, localizedReflectiveConverter.convert(localizedResult, 
Locale.getDefault(), TimeZone.getDefault()));
+                assertEquals(label + " formatted reflection converted", 
source, localizedReflectiveConverter.convert(formattedResult, 
Locale.getDefault(), TimeZone.getDefault(), 
DateTimeConverters.CALENDAR_FORMAT));
+            }
+        } catch (ClassCastException e) {}
     }
 
     @SuppressWarnings("unchecked")
@@ -77,8 +89,8 @@ public class TestDateTimeConverters exte
         // Source class = java.util.Calendar
         DateFormat df = new 
SimpleDateFormat(DateTimeConverters.CALENDAR_FORMAT);
         df.setCalendar(cal);
-        assertConversion("CalendarToLong", new 
DateTimeConverters.CalendarToLong(), cal , currentTime);
-        assertConversion("CalendarToString", new 
DateTimeConverters.CalendarToString(), cal , df.format(cal.getTime()));
+        assertConversion("CalendarToLong", new 
DateTimeConverters.CalendarToLong(), cal, currentTime);
+        assertConversion("CalendarToString", new 
DateTimeConverters.CalendarToString(), cal, df.format(cal.getTime()));
         assertToCollection("CalendarToCollection", cal);
         // Source class = java.util.Date
         assertConversion("DateToLong", new 
DateTimeConverters.GenericDateToLong<java.util.Date>(java.util.Date.class), 
utilDate, currentTime);
@@ -91,11 +103,13 @@ public class TestDateTimeConverters exte
         // Source class = java.sql.Date
         assertConversion("SqlDateToDate", new 
DateTimeConverters.SqlDateToDate(), sqlDate, new 
java.util.Date(sqlDate.getTime()));
         assertConversion("SqlDateToLong", new 
DateTimeConverters.GenericDateToLong<java.sql.Date>(java.sql.Date.class), 
sqlDate, sqlDate.getTime());
+        assertConversion("SqlDateToString", new 
DateTimeConverters.SqlDateToString(), sqlDate, sqlDate.toString());
         assertConversion("SqlDateToTimestamp", new 
DateTimeConverters.SqlDateToTimestamp(), sqlDate, new 
java.sql.Timestamp(sqlDate.getTime()));
         assertToCollection("SqlDateToCollection", sqlDate);
         // Source class = java.sql.Time
         assertConversion("SqlTimeToLong", new 
DateTimeConverters.GenericDateToLong<java.sql.Time>(java.sql.Time.class), 
sqlTime, sqlTime.getTime());
         assertToCollection("SqlTimeToCollection", sqlTime);
+        assertConversion("SqlTimeToString", new 
DateTimeConverters.SqlTimeToString(), sqlTime, sqlTime.toString());
         // Source class = java.sql.Timestamp
         assertConversion("TimestampToLong", new 
DateTimeConverters.GenericDateToLong<java.sql.Timestamp>(java.sql.Timestamp.class),
 timestamp, currentTime);
         assertConversion("TimestampToString", new 
DateTimeConverters.TimestampToString(), timestamp, timestamp.toString());


Reply via email to