This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Re: Bug in SimpleDateFormat, with a patch
- From: Tom Tromey <tromey at redhat dot com>
- To: "H. Väisänen" <hvaisane at joyx dot joensuu dot fi>
- Cc: <java-patches at gcc dot gnu dot org>
- Date: 24 Jul 2003 09:30:15 -0600
- Subject: Re: Bug in SimpleDateFormat, with a patch
- References: <Pine.OSF.4.33.0307240734400.15901-100000@joyds1.joensuu.fi>
- Reply-to: tromey at redhat dot com
>>>>> ">" == H Väisänen <hvaisane@joyx.joensuu.fi> writes:
>> The following program outputs
>> 1-01-01
>> The output should be
>> 0001-01-01
Thanks for the patch and the test case.
I've put the test into Mauve.
I'm checking in a slightly modified version of your patch.
According to the Sun docs, we should truncate the year if the field
size is 2, otherwise we should zero pad.
I'm checking in the modified patch (appended) to libgcj trunk and
classpath.
Tom
Index: ChangeLog
from H. Väisänen <hvaisane@joyx.joensuu.fi>
* java/text/SimpleDateFormat.java (format) [YEAR_FIELD]: Zero pad
unless field size is 2.
Index: java/text/SimpleDateFormat.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/text/SimpleDateFormat.java,v
retrieving revision 1.18
diff -u -r1.18 SimpleDateFormat.java
--- java/text/SimpleDateFormat.java 12 Jun 2003 03:25:31 -0000 1.18
+++ java/text/SimpleDateFormat.java 24 Jul 2003 15:06:03 -0000
@@ -430,11 +430,15 @@
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));
+ // If we have two digits, then we truncate. Otherwise, we
+ // use the size of the pattern, and zero pad.
+ if (p.size == 2)
+ {
+ temp = String.valueOf(calendar.get(Calendar.YEAR));
+ buffer.append(temp.substring(temp.length() - 2));
+ }
else
- buffer.append(temp);
+ withLeadingZeros(calendar.get(Calendar.YEAR), p.size, buffer);
break;
case MONTH_FIELD:
if (p.size < 3)