Is it possible to retrieve a default pattern for a given locale, without casting an object returned by DateFormat.get*Instance()
to a SimpleDateFormat
?
I understand, that in most cases everything will be OK, but there is a note in javadoc
, here: "If you want even more control over the format or parsing, (or want to give your users more control), you can try casting the DateFormat
you get from the factory methods to a SimpleDateFormat
. This will work for the majority of countries; just remember to put it in a try
block in case you encounter an unusual one."
So I wonder, what should I do in case I "encounter an unusual one"?
Related theme.
Code sample:
/**
* Returns '\n'-separated string with available patterns.
* Optional adds appropriate language code to each pattern string.
*
* @param showLanguage Defines if language info is required.
* @return String with available patterns, optional (if showLanguage is set
* to "true") adds appropriate language code to each pattern.
*/
public String getPatternsForAvailableLocales(Boolean... showLanguage) {
/* Array of available locales */
Locale[] locales = DateFormat.getAvailableLocales();
String result = "";
for (Locale locale : locales) {
/* Add language info, if necessary */
if ((showLanguage.length > 0) && (showLanguage[0])) {
result += locale.getLanguage() + '\t';
}
/* Retrieving pattern */
try {
result += ((SimpleDateFormat)
DateFormat.getDateTimeInstance(DateFormat.SHORT,
DateFormat.SHORT, locale)).toPattern();
} catch (ClassCastException e) {
// ******************************** //
// What's up? Is there another way? //
// ******************************** //
}
result += '\n';
}
return result;
}
The pattern itself is unique to SimpleDateFormat which is why you're casting it.
If you find a DateFormat that is not SimpleDateFormat you want to log the exception, recording the class the actual implementation is, and at that point you'll have enough information to decide how to handle the situation.
For now, since there is no pattern don't add this one to the list of results if it is not SimpleDateFormat.
If I can ask, Why are you returning a concatenated String instead of a Collection of Strings? It seems it would be easier to loop through your results.
If the concatenated String is essential, use StringBuilder to build it since you'll create fewer objects and improve performance. It's a good habit to be in when building Strings in a loop.
You can retrieve the date format symbols for a particular language by executing the following Java code:
System.out.println(DateFormatSymbols.getInstance
(new Locale("en_US")).getLocalPatternChars());
System.out.println(DateFormatSymbols.getInstance
(new Locale("nl_NL")).getLocalPatternChars());
In this particular case, Dutch, the date format symbols are the same.
GyMdkHmsSEDFwWahKzZ
GyMdkHmsSEDFwWahKzZ
In the event that the symbols for the foreign language are different, you substitute the foreign language character for the English character in the simple format string.
The DateFormatSymbols
class has existed since at least Java 1.4.2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With