Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6974881

Issue with java.sql.TimeStamp class, valueOf() method with fractional strings

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 6u21
    • core-libs
    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      jdk1.6.0_06

      ADDITIONAL OS VERSION INFORMATION :
      Windows XP - SP3

      A DESCRIPTION OF THE PROBLEM :
      When a String value with fractional seconds is passed to java.sql.Timestamp.valueOf() method , where the fractional seconds length is less than 6, the valueOf method adds trailing zeros which changes the value of the fractional string.

      Eg.,
      public class TimestampTest {
      public static void main(String[] args) {
      Timestamp t = Timestamp.valueOf("2005-1-01 10:20:50.11");
      }
      }

      valueOf - returns nano seconds as - 110000000 - which is not the original value.

      This should instead add leading zeros as done in case of toString() method.

      Code excerpt: - java.sql.Timestamp.java
      -------------------

      // Convert the time; default missing nanos
      if ((firstColon > 0) & (secondColon > 0) &
      (secondColon < time_s.length()-1)) {
      hour = Integer.parseInt(time_s.substring(0, firstColon));
      minute =
      Integer.parseInt(time_s.substring(firstColon+1, secondColon));
      if ((period > 0) & (period < time_s.length()-1)) {
      second =
      Integer.parseInt(time_s.substring(secondColon+1, period));
      nanos_s = time_s.substring(period+1);
      if (nanos_s.length() > 9)
      throw new java.lang.IllegalArgumentException(formatError);
      if (!Character.isDigit(nanos_s.charAt(0)))
      throw new java.lang.IllegalArgumentException(formatError);
      nanos_s = nanos_s + zeros.substring(0,9-nanos_s.length());
       //-----> this should be changed to
       // nanos_s = zeros.substring(0,9- nanos_s.length()) + nanos_s;

      a_nanos = Integer.parseInt(nanos_s);
      } else if (period > 0) {
      throw new java.lang.IllegalArgumentException(formatError);
      } else {
      second = Integer.parseInt(time_s.substring(secondColon+1));
      }
      } else {
      throw new java.lang.IllegalArgumentException();
      }

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      public class TimestampTest {
      public static void main(String[] args) {
      Timestamp t = Timestamp.valueOf("2005-1-01 10:20:50.11");
                                                System.out.println("Nanos: " + t.getNanos());
      }
      }



      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Just '11' or '000000011'
      ACTUAL -
       110000000

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      No error message. Only the value is corrupted

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      Please see the description section
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Write our own implementation to handle the timestamp fractional seconds.

      SUPPORT :
      YES

            lancea Lance Andersen
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: