JDK-4491595 : java.text.SimpleDateFormat incorrectly works with patterns
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.text
  • Affected Version: 1.4.0
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: solaris_2.6
  • CPU: sparc
  • Submitted: 2001-08-14
  • Updated: 2001-08-17
  • Resolved: 2001-08-17
Related Reports
Relates :  
Description

Name: ooR10001			Date: 08/14/2001


java.text.SimpleDateFormat(String, Locale) throws IllegalArgumentException when 
pattern contain characters which is can not be interpreted as meta-symbols in 
pattern. This behavior contradicts with current JavaDoc which says:

----------- JavaDoc ---------------
Date and time formats are specified by date and time pattern strings. Within 
date and time pattern strings, unquoted letters from 'A' to 'Z' and from 'a' to 
'z' are interpreted as pattern letters representing the components of a date or 
time string. Text can be quoted using single quotes (') to avoid interpretation. 
"''" represents a single quote. All other characters are not interpreted;
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
they're simply copied into the output string during formatting or matched
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
against the input string during parsing.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
----------------------------------

Such characters should not cause an exception when pattern being verified.

Following minimized test shows the bug:

------------- test.java --------------
import java.text.SimpleDateFormat;
import java.util.Locale;

public class test {

  public static void main(String[] args) {
    SimpleDateFormat f = 
        new SimpleDateFormat("yyyy.MM.dd e hh.mm.ss zzz", Locale.ENGLISH);
    System.out.println(f.toPattern());
  }

}
--------------------------------------

Output:
-------------------------------
% java -version

java version "1.4.0-beta_refresh"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta_refresh-b75)
Java HotSpot(TM) Client VM (build 1.4.0-beta_refresh-b75, mixed mode)

% java test

Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern 
character 'e'
        at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:678)
        at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:497)
        at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:446)
        at t1.main(test.java:7)

--------------------------------------------------------
======================================================================

Comments
EVALUATION That part of the API doc means that all the characters 'a' to 'z' and 'A' to 'Z' of the Unicode Basic Latin block are reserved as pattern letters. The difference between Merlin b75 and 1.3.1 is the spec change for the 4326988 fix approved by CCC. From b75, the SimpleDateFormat constructor checks the given pattern and throws IllegalArgumentException if it's invalid. Before b75, the pattern checking was performed in format() and parse(). For example, the following program fails in format() in 1.3.1. 'e' must be quoted. -- import java.text.SimpleDateFormat; import java.util.*; public class test { public static void main(String[] args) { SimpleDateFormat f = new SimpleDateFormat("yyyy.MM.dd e hh.mm.ss zzz", Locale.ENGLISH); System.out.println(f.format(new Date())); System.out.println(f.toPattern()); } } -- % /usr/local/java/jdk1.3.1/solsparc/bin/java test Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern character 'e' at java.text.SimpleDateFormat.subFormat(SimpleDateFormat.java:477) at java.text.SimpleDateFormat.format(SimpleDateFormat.java:410) at java.text.DateFormat.format(DateFormat.java:305) at test.main(test.java:9) ###@###.### 2001-08-17
17-08-2001