This is the mail archive of the java@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: SimpleDateFormat parse bug: doesn't like embedded spaces


Scott Gilbertson wrote:
I found a difference between SimpleDateFormat.parse in GCJ/Classpath vs.
Sun.  Tiny test program attached.

The attached program runs the following strings:
    "2:03:34"
    "2: 3:34"

Through this SimpleDateFormat:
    new SimpleDateFormat ("H':'m':'s")

The result with GCJ (from svn a Jan 24) is different from Sun JRE 1.4.2_08\

Hmm, another undocumented behaviour of Sun's class libraries. The attached patch should make us compatible. We'll need to add a mauve test for this, too.


Bryce

2006-02-03  Bryce McKinlay  <mckinlay@redhat.com>

        * java/text/SimpleDateFormat.java (parse): Ignore leading whitespace
	characters in numeric fields.

Index: classpath/java/text/SimpleDateFormat.java
===================================================================
--- classpath/java/text/SimpleDateFormat.java	(revision 110474)
+++ classpath/java/text/SimpleDateFormat.java	(working copy)
@@ -1086,7 +1086,20 @@
 		  numberFormat.setMaximumIntegerDigits(fmt_count);
 		if (maybe2DigitYear)
 		  index = pos.getIndex();
-		Number n = numberFormat.parse(dateStr, pos);
+		
+		// Ignore leading whitespace, for compatibility.
+		try
+		{
+	          int wsIndex = pos.getIndex();
+	          while (Character.isWhitespace(dateStr.charAt(wsIndex)))
+		    pos.setIndex(++wsIndex);
+		}
+		catch (StringIndexOutOfBoundsException ignored)
+		{
+		  return null;
+		}
+
+		Number n = numberFormat.parse(dateStr, pos);		
 		if (pos == null || ! (n instanceof Long))
 		  return null;
 		value = n.intValue() + offset;
@@ -1177,7 +1190,7 @@
         pos.setErrorIndex(pos.getIndex());
 	return null;
       }
-      }
+ }
 
   /**
    * <p>

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]