Bug in SimpleDateFormat, with a patch
H. Väisänen
hvaisane@joyx.joensuu.fi
Thu Jul 24 04:52:00 GMT 2003
The following program outputs
1-01-01
when compiled with gcj version 3.3 like this
> gcj --main=Foobar Foobar.java -o foobar
> gcj --version
gcj (GCC) 3.3
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
I think gcj output is incorrect since it does not have leading zeros.
The output should be
0001-01-01
************************************** The Program
import java.util.GregorianCalendar;
public class Foobar {
public static void main (String[] args)
{
java.text.DateFormat f = new java.text.SimpleDateFormat ("yyyy-MM-dd");
GregorianCalendar g = new GregorianCalendar (1, 0, 1, 12, 0, 0);
System.out.println (f.format(g.getTime()));
}
}
*************************************
The error is in java.text.SimpleDateFormat
Here is the patch
> diff -3cp SimpleDateFormat.java.old SimpleDateFormat.java
*** SimpleDateFormat.java.old 2003-07-23 17:34:39.000000000 +0300
--- SimpleDateFormat.java 2003-07-24 06:58:28.000000000 +0300
*************** public class SimpleDateFormat extends Da
*** 416,427 ****
buffer.append(formatData.eras[calendar.get(Calendar.ERA)]);
break;
case YEAR_FIELD:
! temp = String.valueOf(calendar.get(Calendar.YEAR));
! if (p.size < 4)
! buffer.append(temp.substring(temp.length()-2));
! else
! buffer.append(temp);
! break;
case MONTH_FIELD:
if (p.size < 3)
withLeadingZeros(calendar.get(Calendar.MONTH)+1,p.size,buffer);
--- 416,429 ----
buffer.append(formatData.eras[calendar.get(Calendar.ERA)]);
break;
case YEAR_FIELD:
! if (p.size < 3)
! {
! temp = String.valueOf(calendar.get(Calendar.YEAR));
! buffer.append(temp.substring(temp.length()-2));
! }
! else
! withLeadingZeros(calendar.get(Calendar.YEAR),p.size,buffer);
! break;
case MONTH_FIELD:
if (p.size < 3)
withLeadingZeros(calendar.get(Calendar.MONTH)+1,p.size,buffer);
More information about the Java-patches
mailing list