Unfortunately, the documentation on how to handle localized templates is terrible. Therefore, I studied the source code and conducted my own investigations. Result:
The SimpleDateFormat constructor that accepts a pattern string applies only to non-localized pattern characters, the definition of which is documented as indicated in the javadoc header of the SimpleDateFormat class. These non-localized wildcards are also defined as constant in DateTimeFormatSymbols :
static final String patternChars = "GyMdkHmsSEDFwWahKzZYuXL";
To use localized templates , three steps are required (for example, "tt.MM.uuuu", which you consider German but not German, rather it is "TT.MM. JJJJ" - an example of incorrect JDK resources):
- Define localized template characters through
DateFormatSymbols.setLocalPatternChars(...) . - Use custom date format characters in a
SimpleDateFormat object. - Apply a localized datetime template using
SimpleDateFormat.applyLocalizedPattern(...)
Then, the localized template will be translated into the internal and official definition of the template symbol.
Usage example (using the correct German template TT.MM.JJJJ):
SimpleDateFormat sdf = new SimpleDateFormat(); // uses default locale (here for Germany) System.out.println(sdf.toPattern()); // dd.MM.yy HH:mm System.out.println(sdf.toLocalizedPattern()); // tt.MM.uu HH:mm DateFormatSymbols dfs = DateFormatSymbols.getInstance(Locale.GERMANY); dfs.setLocalPatternChars("GJMTkHmsSEDFwWahKzZYuXL"); sdf.setDateFormatSymbols(dfs); sdf.applyLocalizedPattern("TT.MM.JJJJ"); System.out.println(sdf.toPattern()); // dd.MM.yyyy System.out.println(sdf.toLocalizedPattern()); // TT.MM.JJJJ System.out.println(sdf.format(new Date())); // 20.06.2016
Side note: I changed the corresponding template patterns y and d to J and T in the line "GyMdkHmsSEDFwWahKzZYuXL" to make a localized definition.
Unfortunately, the JDK resources are obviously not reliable, so my personal opinion is that the whole function can only be used inconveniently and in practice is not very useful.
source share