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)
======================================================================
- duplicates
-
JDK-8058230 Improve java.sql toString formatting
-
- Resolved
-