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

Remove brittleness and enhance performance of java.sql.Date.toString()

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.0
    • core-libs
    • x86
    • windows_2000



      Name: jl125535 Date: 01/27/2003


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

      A DESCRIPTION OF THE PROBLEM :
      java.sql.Date toString() method enhancement. Current code
      is brittle due to need to index into char buffer. Current
      code is also double writing characters in buffer: first
      what creating the buffer and copying in initial template,
      then when it goes back to over write template with actual
      data. Suggested enhancement removes need for indexing and
      only writes each character once.

      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      Current source:
          public String toString () {
      int year = super.getYear() + 1900;
      int month = super.getMonth() + 1;
      int day = super.getDate();

              char buf[] = "2000-00-00".toCharArray();
              buf[0] = Character.forDigit(year/1000,10);
              buf[1] = Character.forDigit((year/100)%10,10);
              buf[2] = Character.forDigit((year/10)%10,10);
              buf[3] = Character.forDigit(year%10,10);
              buf[5] = Character.forDigit(month/10,10);
              buf[6] = Character.forDigit(month%10,10);
              buf[8] = Character.forDigit(day/10,10);
              buf[9] = Character.forDigit(day%10,10);

      return new String(buf);
          }

        Suggested enhancement:
          public String toString () {
      int year = super.getYear() + 1900;
      int month = super.getMonth() + 1;
      int day = super.getDate();

              char buf[] = {
      Character.forDigit(year/1000,10),
      Character.forDigit((year/100)%10,10),
      Character.forDigit((year/10)%10,10),
      Character.forDigit(year%10,10),
      '-',
      Character.forDigit(month/10,10),
      Character.forDigit(month%10,10),
      '-',
      Character.forDigit(day/10,10),
      Character.forDigit(day%10,10),

      return new String(buf);
          }

      ---------- END SOURCE ----------
      (Review ID: 153577)
      ======================================================================

            Unassigned Unassigned
            jleesunw Jon Lee (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: