harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From py...@apache.org
Subject svn commit: r441070 - in /incubator/harmony/enhanced/classlib/trunk/modules/sql/src: main/java/java/sql/Timestamp.java test/java/org/apache/harmony/sql/tests/java/sql/TimestampTest.java
Date Thu, 07 Sep 2006 12:29:57 GMT
Author: pyang
Date: Thu Sep  7 05:29:56 2006
New Revision: 441070

URL: http://svn.apache.org/viewvc?view=rev&rev=441070
Log:
Patch applied for HARMONY-1400 ([classlib][sql] The behavior of java.util.sql.Timestamp.toString()
is different from RI)

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/sql/src/main/java/java/sql/Timestamp.java
    incubator/harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/java/sql/TimestampTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/sql/src/main/java/java/sql/Timestamp.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/sql/src/main/java/java/sql/Timestamp.java?view=diff&rev=441070&r1=441069&r2=441070
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/sql/src/main/java/java/sql/Timestamp.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/sql/src/main/java/java/sql/Timestamp.java
Thu Sep  7 05:29:56 2006
@@ -292,12 +292,8 @@
      * @return A string representing the instant defined by the Timestamp, in
      *         JDBC Timestamp escape format
      */
+    @Override
     public String toString() {
-        // A SimpleDateFormat will lay out everything except the nanosecond
-        // value
-        SimpleDateFormat dateFormat = new SimpleDateFormat(
-                "yyyy-MM-dd HH:mm:ss"); //$NON-NLS-1$
-
         /*
          * Use a DecimalFormat to lay out the nanosecond value as a simple
          * string of 9 integers, with leading Zeros
@@ -307,10 +303,29 @@
         decimalFormat.setMaximumIntegerDigits(9);
         String theNanos = decimalFormat.format(nanos);
         theNanos = stripTrailingZeros(theNanos);
-        // Concatenate the nanosecond value with a dot - and return
-        return (dateFormat.format(this) + '.' + theNanos);
+        
+        String year = format((getYear() + 1900), 4);
+        String month = format((getMonth() + 1), 2);
+        String date = format(getDate(), 2);
+        String hours = format(getHours(), 2);
+        String minutes = format(getMinutes(), 2);
+        String seconds = format(getSeconds(), 2);
+
+        return year + '-' + month + '-' + date + ' ' + hours + ':' + minutes
+                + ':' + seconds + '.' + theNanos;
     }
 
+    /*
+     * Private method to format the time
+     */
+    private String format(int date, int digits) {
+        StringBuilder dateStringBuffer = new StringBuilder(String.valueOf(date));
+        while (dateStringBuffer.length() < digits) {
+            dateStringBuffer = dateStringBuffer.insert(0,'0');
+        }
+        return dateStringBuffer.toString();
+    }
+    
     /*
      * Private method to strip trailing '0' characters from a string. @param
      * inputString the starting string @return a string with the trailing zeros

Modified: incubator/harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/java/sql/TimestampTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/java/sql/TimestampTest.java?view=diff&rev=441070&r1=441069&r2=441070
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/java/sql/TimestampTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/java/sql/TimestampTest.java
Thu Sep  7 05:29:56 2006
@@ -617,5 +617,23 @@
         Timestamp object = new Timestamp(100L);
         SerializationTest.verifyGolden(this, object);
     }
+    
+    /**
+     * @tests java.sql.Timestamp#toString()
+     */
+    public void test_toString() {
 
+        Timestamp t1 = new Timestamp(Long.MIN_VALUE);
+        assertEquals("292278994-08-17 07:12:55.192", t1.toString()); //$NON-NLS-1$
+
+        Timestamp t2 = new Timestamp(Long.MIN_VALUE + 1);
+        assertEquals("292278994-08-17 07:12:55.193", t2.toString()); //$NON-NLS-1$
+
+        Timestamp t3 = new Timestamp(Long.MIN_VALUE + 807);
+        assertEquals("292278994-08-17 07:12:55.999", t3.toString()); //$NON-NLS-1$
+
+        Timestamp t4 = new Timestamp(Long.MIN_VALUE + 808);
+        assertEquals("292269055-12-02 16:47:05.0", t4.toString()); //$NON-NLS-1$
+    }
+ 
 } // end class TimestampTest



Mime
View raw message