United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4823811 : [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation

Details
Type:
Bug
Submit Date:
2003-02-25
Status:
Closed
Updated Date:
2010-07-09
Project Name:
JDK
Resolved Date:
2011-03-08
Component:
core-libs
OS:
solaris_2.3,windows_xp
Sub-Component:
java.text
CPU:
x86,sparc
Priority:
P4
Resolution:
Fixed
Affected Versions:
1.3.0,1.4.1,5.0
Fixed Versions:

Related Reports
Backport:
Backport:
Backport:
Backport:
Backport:
Duplicate:
Relates:

Sub Tasks

Description

Name: nt126004			Date: 02/25/2003


FULL PRODUCT VERSION :
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)


FULL OPERATING SYSTEM VERSION :
Microsoft Windows XP [Version 5.1.2600]

ADDITIONAL OPERATING SYSTEMS :
I think this also happen on Solaris, but I only had the
IBM JDK to test with there.


A DESCRIPTION OF THE PROBLEM :
Because of the "greedy" way the numeric parsing works
inside SimpleDateFormat.parse, it's not possible to embed
certain literal characters.  Here is an example:

In Locale "ar_EG" (Arabic Egypt), you can't use literal
dashes as separators for date components because the first
dash gets "eaten" as part of parsing the first integer
(Arabic locales put numeric signs to the right of the
digits).

It doesn't help if you surround the dash with 'single
quotes' in the pattern.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the source code provided below.  I don't know if you
have to do something to make sure you have a Locale
for "ar_EG" installed on your machine.

EXPECTED VERSUS ACTUAL BEHAVIOR :
Expect to parse the date into constituent parts, but
instead get an exception.

CAUGHT java.text.ParseException: Unparseable date: "2002-
12,83.567", errorOffset = 5


ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.text.ParseException: Unparseable date: "2002-12,83.567"
	at java.text.DateFormat.parse(DateFormat.java:324)
	at org.carpenter.bill.CreTe.demoDateBug(CreTe.java:3093)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.text.*;
import java.util.*;
public class Test {
	public static void main(String[] args) {
		String s = "2002-12,83.567";
        System.out.println("DATE 0: " + s);
        String pattern = "yyyy'-'mm','ss'.'SSS";
        SimpleDateFormat sdf;
        Date date = null;


        Locale.setDefault(new Locale("ar", "EG"));
        sdf = new SimpleDateFormat(pattern);
        try
        {
            date = sdf.parse(s);
            System.out.println("DATE 1: " + date + " --> " + date.getTime());
        }
        catch (ParseException pe)
        {
            System.out.println("CAUGHT " + pe + ", errorOffset = " +
pe.getErrorOffset());
            pe.printStackTrace();
        }
	}
}
---------- END SOURCE ----------

CUSTOMER WORKAROUND :
I have not found one.
(Review ID: 181728) 
======================================================================

                                    

Comments
EVALUATION



Name: nl37777			Date: 03/14/2003

Problem exists in all of 1.1.8, 1.2.2, 1.3.1, 1.4, 1.4.1.


======================================================================
                                     
2004-08-06
WORK AROUND

(1) Temporarily replace all "-"s in the pattern and strings to be parsed with another delimiter, like "/". The test case given in the Description can be modified to:

import java.text.*;
import java.util.*;

public class Test {
    public static void main(String[] args) {
	String s = "2002-12,83.567";
        System.out.println("DATE 0: " + s);
        String pattern = "yyyy'-'mm','ss'.'SSS";
        SimpleDateFormat sdf;
        Date date = null;


        Locale.setDefault(new Locale("ar", "EG"));
        sdf = new SimpleDateFormat(pattern.replace("-", "/")); // modified
        try {
            date = sdf.parse(s.replace("-", "/")); // modified
            System.out.println("DATE 1: " + date + " --> " + date.getTime());
        } catch (ParseException pe) {
            System.out.println("CAUGHT " + pe + ", errorOffset = " +
			       pe.getErrorOffset());
            pe.printStackTrace();
        }
    }
}

This one produces:

DATE 0: 2002-12,83.567
DATE 1: Tue Jan 01 00:13:23 UTC 2002 --> 1009844003567

(2) When creating a SimpleDateFormat, specify Locale.ENGLISH (or Locale.ROOT in JDK 6) explicitly if the format pattern doesn't involve any Arabic strings (e.g., month names).

import java.text.*;
import java.util.*;

public class Test2 {
    public static void main(String[] args) {
	String s = "2002-12,83.567";
        System.out.println("DATE 0: " + s);
        String pattern = "yyyy'-'mm','ss'.'SSS";
        SimpleDateFormat sdf;
        Date date = null;


        Locale.setDefault(new Locale("ar", "EG"));
        sdf = new SimpleDateFormat(pattern, Locale.ROOT); // modified
        try {
            date = sdf.parse(s);
            System.out.println("DATE 1: " + date + " --> " + date.getTime());
        } catch (ParseException pe) {
            System.out.println("CAUGHT " + pe + ", errorOffset = " +
			       pe.getErrorOffset());
            pe.printStackTrace();
        }
    }
}

This one produces the following as well.

DATE 0: 2002-12,83.567
DATE 1: Tue Jan 01 00:13:23 UTC 2002 --> 1009844003567
                                     
2008-07-22



Hardware and Software, Engineered to Work Together