commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adri...@apache.org
Subject svn commit: r922774 - /commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/DateTimeConverters.java
Date Sun, 14 Mar 2010 06:34:52 GMT
Author: adrianc
Date: Sun Mar 14 06:34:52 2010
New Revision: 922774

URL: http://svn.apache.org/viewvc?rev=922774&view=rev
Log:
Built out the Date/Time converters.

Modified:
    commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/DateTimeConverters.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=922774&r1=922773&r2=922774&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 Mar 14 06:34:52 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;
@@ -29,20 +30,6 @@ import java.util.TimeZone;
 
 /** Date/time Converter classes. */
 public class DateTimeConverters implements ConverterLoader {
-    /**
-     * JDBC escape format for java.sql.Date conversions.
-     */
-    public static final String DATE_FORMAT = "yyyy-MM-dd";
-
-    /**
-     * JDBC escape format for java.sql.Timestamp conversions.
-     */
-    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
-
-    /**
-     * JDBC escape format for java.sql.Time conversions.
-     */
-    public static final String TIME_FORMAT = "HH:mm:ss";
 
     /**
      * Returns an initialized DateFormat object.
@@ -203,46 +190,46 @@ public class DateTimeConverters implemen
         }
 
         public Calendar convert(Long obj, Locale locale, TimeZone timeZone, String formatString)
throws ConversionException {
-            return convert(obj, Locale.getDefault(), TimeZone.getDefault());
+            return convert(obj, locale, timeZone);
         }
     }
 
-    public static class NumberToDate extends AbstractConverter<Number, Date> {
-        public NumberToDate() {
-            super(Number.class, Date.class);
+    public static class LongToDate extends AbstractConverter<Long, Date> {
+        public LongToDate() {
+            super(Long.class, Date.class);
         }
 
-        public Date convert(Number obj) throws ConversionException {
+        public Date convert(Long obj) throws ConversionException {
              return new Date(obj.longValue());
         }
     }
 
-    public static class NumberToSqlDate extends AbstractConverter<Number, java.sql.Date>
{
-        public NumberToSqlDate() {
-            super(Number.class, java.sql.Date.class);
+    public static class LongToSqlDate extends AbstractConverter<Long, java.sql.Date>
{
+        public LongToSqlDate() {
+            super(Long.class, java.sql.Date.class);
         }
 
-        public java.sql.Date convert(Number obj) throws ConversionException {
+        public java.sql.Date convert(Long obj) throws ConversionException {
              return new java.sql.Date(obj.longValue());
         }
     }
 
-    public static class NumberToSqlTime extends AbstractConverter<Number, java.sql.Time>
{
-        public NumberToSqlTime() {
-            super(Number.class, java.sql.Time.class);
+    public static class LongToSqlTime extends AbstractConverter<Long, java.sql.Time>
{
+        public LongToSqlTime() {
+            super(Long.class, java.sql.Time.class);
         }
 
-        public java.sql.Time convert(Number obj) throws ConversionException {
+        public java.sql.Time convert(Long obj) throws ConversionException {
              return new java.sql.Time(obj.longValue());
         }
     }
 
-    public static class NumberToTimestamp extends AbstractConverter<Number, java.sql.Timestamp>
{
-        public NumberToTimestamp() {
-            super(Number.class, java.sql.Timestamp.class);
+    public static class LongToTimestamp extends AbstractConverter<Long, java.sql.Timestamp>
{
+        public LongToTimestamp() {
+            super(Long.class, java.sql.Timestamp.class);
         }
 
-        public java.sql.Timestamp convert(Number obj) throws ConversionException {
+        public java.sql.Timestamp convert(Long obj) throws ConversionException {
              return new java.sql.Timestamp(obj.longValue());
         }
     }
@@ -263,6 +250,16 @@ public class DateTimeConverters implemen
         }
     }
 
+    public static class SqlDateToLong extends AbstractConverter<java.sql.Date, Long>
{
+        public SqlDateToLong() {
+            super(java.sql.Date.class, Long.class);
+        }
+
+        public Long convert(java.sql.Date obj) throws ConversionException {
+             return obj.getTime();
+        }
+    }
+
     public static class SqlDateToSet extends GenericSingletonToSet<java.sql.Date> {
         public SqlDateToSet() {
             super(java.sql.Date.class);
@@ -311,6 +308,16 @@ public class DateTimeConverters implemen
         }
     }
 
+    public static class SqlTimeToLong extends AbstractConverter<java.sql.Time, Long>
{
+        public SqlTimeToLong() {
+            super(java.sql.Time.class, Long.class);
+        }
+
+        public Long convert(java.sql.Time obj) throws ConversionException {
+             return obj.getTime();
+        }
+    }
+
     public static class SqlTimeToSet extends GenericSingletonToSet<java.sql.Time> {
         public SqlTimeToSet() {
             super(java.sql.Time.class);
@@ -332,6 +339,11 @@ public class DateTimeConverters implemen
             super(java.sql.Time.class, String.class);
         }
 
+        @Override
+        public String convert(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(formatString, timeZone, locale);
             return df.format(obj);
@@ -369,6 +381,16 @@ public class DateTimeConverters implemen
             super(String.class, Date.class);
         }
 
+        @Override
+        public Date convert(String obj) throws ConversionException {
+            try {
+                DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
+                return df.parse(obj);
+            } catch (ParseException e) {
+                throw new ConversionException(e);
+            }
+        }
+
         public Date convert(String obj, Locale locale, TimeZone timeZone, String formatString)
throws ConversionException {
             DateFormat df = toDateTimeFormat(formatString, timeZone, locale);
             try {
@@ -386,12 +408,7 @@ public class DateTimeConverters implemen
 
         @Override
         public java.sql.Date convert(String obj) throws ConversionException {
-            DateFormat df = new SimpleDateFormat(DATE_FORMAT);
-            try {
-                return new java.sql.Date(df.parse(obj).getTime());
-            } catch (ParseException e) {
-                throw new ConversionException(e);
-            }
+            return java.sql.Date.valueOf(obj);
         }
 
         public java.sql.Date convert(String obj, Locale locale, TimeZone timeZone, String
formatString) throws ConversionException {
@@ -411,12 +428,7 @@ public class DateTimeConverters implemen
 
         @Override
         public java.sql.Time convert(String obj) throws ConversionException {
-            DateFormat df = new SimpleDateFormat(TIME_FORMAT);
-            try {
-                return new java.sql.Time(df.parse(obj).getTime());
-            } catch (ParseException e) {
-                throw new ConversionException(e);
-            }
+            return java.sql.Time.valueOf(obj);
         }
 
         public java.sql.Time convert(String obj, Locale locale, TimeZone timeZone, String
formatString) throws ConversionException {
@@ -436,12 +448,7 @@ public class DateTimeConverters implemen
 
         @Override
         public Timestamp convert(String obj) throws ConversionException {
-            DateFormat df = new SimpleDateFormat(DATE_TIME_FORMAT);
-            try {
-                return new java.sql.Timestamp(df.parse(obj).getTime());
-            } catch (ParseException e) {
-                throw new ConversionException(e);
-            }
+            return java.sql.Timestamp.valueOf(obj);
         }
 
         public java.sql.Timestamp convert(String obj, Locale locale, TimeZone timeZone, String
formatString) throws ConversionException {
@@ -480,6 +487,16 @@ public class DateTimeConverters implemen
         }
     }
 
+    public static class TimestampToLong extends AbstractConverter<java.sql.Timestamp,
Long> {
+        public TimestampToLong() {
+            super(java.sql.Timestamp.class, Long.class);
+        }
+
+        public Long convert(java.sql.Timestamp obj) throws ConversionException {
+             return obj.getTime();
+        }
+    }
+
     public static class TimestampToSet extends GenericSingletonToSet<Timestamp> {
         public TimestampToSet() {
             super(Timestamp.class);
@@ -496,6 +513,22 @@ public class DateTimeConverters implemen
         }
     }
 
+    public static class TimestampToString extends GenericLocalizedConverter<java.sql.Timestamp,
String> {
+        public TimestampToString() {
+            super(java.sql.Timestamp.class, String.class);
+        }
+
+        @Override
+        public String convert(java.sql.Timestamp obj) throws ConversionException {
+            return obj.toString();
+        }
+
+        public String convert(java.sql.Timestamp obj, Locale locale, TimeZone timeZone, String
formatString) throws ConversionException {
+            DateFormat df = toDateFormat(formatString, timeZone, locale);
+            return df.format(obj);
+        }
+    }
+
     public static class TimeZoneToString extends AbstractConverter<TimeZone, String>
{
         public TimeZoneToString() {
             super(TimeZone.class, String.class);



Mime
View raw message