SimpleDateFormat throws exception with valid inputs - ideas?

Scott Gilbertson (sent from home)
Fri May 16 17:53:00 GMT 2003

I've found the problem and have a fix.  I'll submit a patch when I get back
to the office (Tuesday).

Problem details are as follows:

SimpleDateFormat.parse is thrown off by the decimal point between the
seconds an milliseconds field.  My sample input string is:
    "05/24/2002 14:30:53.700"
Using format string:
    "MM'/'dd'/'yyyy' 'H':'m':'s'.'SSS"

SimpleDateFormat.parse returns null here:
704: if (is_numeric)
705:   {
706:     numberFormat.setMinimumIntegerDigits(fmt_count);
707:     if (maybe2DigitYear)
708:       index = pos.getIndex();
709:     Number n = numberFormat.parse(dateStr, pos);
710:     if (pos == null || ! (n instanceof Long))
711:       return null; <<<<***THIS IS THE SPOT***
712:     value = n.intValue() + offset;
713:   }

    n = java.lang.Double (53.7)
    pos.index = 23
    calendarField = 13 = Calendar.SECOND

So it apparently thinks the seconds field is "53.700".  That's a bug, since
I specified the decimal point as a separator between the seconds and
milliseconds field, so the seconds field should be java.lang.Long (53) and
the milliseconds should be java.lang.Long (700).  I suspect some of the
examples in the 1.4.0 javadoc ("yyyy.MM.dd G 'at' HH:mm:ss z"  and
"yyyyy.MMMMM.dd GGG hh:mm aaa") would also fail, but I havn't tried them.

I added numberFormat.setParseIntegerOnly(true) to each of the
SimpleDateFormat constructors.  Fixed - the test program runs now.  I think
this fix is OK.  Although the JDK javadoc doesn't explicitly say all numeric
fields are integer, it kind of implies that by having examples with periods
between the fields.  Furthermore, the code exerpt above requires the fields
to be integer, so there's no point looking for floating-point fields.

This message is in reply to my earlier one,
Exerpt from original message:
> The attached program runs OK on the JRE but generates an exception on GCJ
> (trunk cvs fetch from a few weeks ago).  I started to look through
> SimpleDateFormat to see if I could spot what was happening, but I'm not at
> all familiar with that class.  Does anybody have an idea of what's going
> or some tips to debug it?  Thanks.

More information about the Java mailing list