I'm trying to understand some SimpleDateFormat code. In particular I'm trying to use localized pattern strings in SimpleDateFormat. From the javadoc:
SimpleDateFormat also supports localized date and time pattern strings. In these strings, the pattern letters described above may be replaced with other, locale dependent, pattern letters.
It also specifies a SimpleDateFormat(String pattern, DateFormatSymbols formatSymbols)
constructor:
Constructs a SimpleDateFormat using the given pattern and date format symbols.
However, although getLocalPatternChars()
instance is presenting the expected pattern characters, SimpleDateFormat's constructors are rejecting patterns containing those characters:
public void run() {
Locale loc = new Locale("de", "de");
DateFormatSymbols dfs = new DateFormatSymbols(loc);
String sym = dfs.getLocalPatternChars();
System.out.println(sym);
SimpleDateFormat datefmt = new SimpleDateFormat("tt.MM.uuuu", dfs);
}
produces output:
GuMtkHmsSEDFwWahKzZ
Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern character 't'
at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:845)
...
I get the same output if I replace the last line with "... new SimpleDateFormat("tt.MM.uuuu", loc);
".
On the other hand, if I create a SimpleDateFormat instance using any Anglicized pattern string, then call "applyLocalizedPattern("tt.MM.uuuu")
", the localized pattern is accepted.
So it appears that one cannot use a localized pattern string in SimpleDateFormat's constructors, and need this two-step initialization. Is this intentional behaviour?
translatePattern
(applyLocalizedPattern
does that), so this is either a bug or a bad JavaDoc which doesn't clearly explain the usage of the constructor. – Trousseau