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

Possibly more efficient java.sql.Timestamp#toString() code

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 5.0
    • core-libs
    • sparc
    • solaris_9



      Name: gm110360 Date: 02/11/2004


      FULL PRODUCT VERSION :
      java version "1.5.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta-b32c)
      Java HotSpot(TM) Client VM (build 1.5.0-beta-b32c, mixed mode)

      A DESCRIPTION OF THE PROBLEM :
      In jdk 1.5.0 beta 1's java.sql.Timestamp#toString() the code:

      // do a string buffer here instead.
      timestampBuf = new StringBuffer();
      timestampBuf.append(yearString);
      timestampBuf.append("-");
      timestampBuf.append(monthString);
      timestampBuf.append("-");
      timestampBuf.append(dayString);
      timestampBuf.append(" ");
      timestampBuf.append(hourString);
      timestampBuf.append(":");
      timestampBuf.append(minuteString);
      timestampBuf.append(":");
      timestampBuf.append(secondString);
      timestampBuf.append(".");
      timestampBuf.append(nanosString);

      Would be much more efficient as:

      // do a string buffer here instead.
      timestampBuf = new StringBuffer(20 + nanosString.length());
      if (year < 1000) {
      // Add leading zeros
      yearString = "" + year;
      timestampBuf.append(zeros.substring(0, (4-yearString.length())))
               }
        timestampBuf.append( year);

      timestampBuf.append('-');
      if (month < 10) {
        timestampBuf.append('0');
               }
        timestampBuf.append(month);

      timestampBuf.append('-');
      if (day < 10) {
        timestampBuf.append('0');
               }
        timestampBuf.append(day);

      timestampBuf.append(' ');
      if (hour < 10) {
        timestampBuf.append('0');
               }
        timestampBuf.append(hour);

      timestampBuf.append(':');
      if (minute < 10) {
        timestampBuf.append('0');
               }
        timestampBuf.append(minute);

      timestampBuf.append(':');
      if (second < 10) {
        timestampBuf.append('0');
               }
        timestampBuf.append(second);

      timestampBuf.append('.');
      timestampBuf.append(nanosString);

      The advantages of this code are numerous and hopefully clearly obvious.


      REPRODUCIBILITY :
      This bug can be reproduced always.
      (Incident Review ID: 238419)
      ======================================================================

            lancea Lance Andersen
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: