[kaffe] Bug report: java.text.SimpleDateFormat#parse

Ito Kazumitsu ito.kazumitsu at hidec.co.jp
Mon Jan 24 23:12:48 PST 2005


Hi,

You might have found that test/regression/DateFormatTest.java fails
recently.

This is becuase of a bug in java.text.SimpleDateFormat#parse.
The following program shows the bug.

import java.text.SimpleDateFormat;

public class Z
{
    public static void main(String args[])
	throws Exception
    {
	SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmssSSSz");
	// System.out.println(sdf.parse("041028130027000GMT+00:00"));
	System.out.println(sdf.parse("041028130027000GMT+04:00"));
    }
}

This program prints (with -Duser.timezone=JST, JST=GMT+09:00):
Thu Oct 28 09:00:27 JDT 2004

instead of the expected result:
Thu Oct 28 18:00:27 JST 2004

The difference between "JDT" and "JST" has already been reported in

  http://www.kaffe.org/pipermail/kaffe/2005-January/101286.html

And the difference between 09:00 and 18:00 is caused by the following
bug in java/text/SimpleDateFormat.java

   842			is_numeric = false;
   843			calendar_field = Calendar.DST_OFFSET;
   844			String[][] zoneStrings = formatData.getZoneStrings();
   845			int zoneCount = zoneStrings.length;
   846			int index = pos.getIndex();
   847			boolean found_zone = false;
   848			simpleOffset = computeOffset(dateStr.substring(index));
   849			if (simpleOffset != null)
   850			  {
   851			    found_zone = true;
   852			    saw_timezone = true;
   853			    offset = simpleOffset.intValue();
   854			  }

At this point, "GMT+04:00" has been parsed and offset gets
14400000 milisoconds (4 hours).

This offset should be set to the calendar's Calendar.ZONE_OFFSET
but java/text/SimpleDateFormat.java fails to do so.  Not only
failing to set the offset,  it does something unwanted.

   941		    else
   942		      value = offset;

   953		    // Assign the value and move on.
   954		    calendar.set(calendar_field, value);
 
This sets the offset value to the calendar's Calendar.DST_OFFSET.

This make the wrong result.

         Parsed          Zone offset  DST offset  GMT     Result in JST
Correct  13:00GMT+04:00  04:00        -           09:00   18:00
Wrong    13:00GMT+04:00  -            04:00       00:00*  09:00

    * 00:00 = 13:00 - 09:00 (default zone offset)
                    - 04:00 (DST offset)



More information about the kaffe mailing list