DateFormat.get*Instance()
によって返されたオブジェクトを にキャストせずに、特定のロケールのデフォルト パターンを取得することは可能SimpleDateFormat
ですか?
ほとんどの場合、すべて問題ないことは理解していますがjavadoc
、ここにメモがあります。ファクトリメソッドDateFormat
からSimpleDateFormat
. _try
では、「いつもと違うものに遭遇」した場合はどうすればいいのだろうか?
コードサンプル:
/**
* 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;
}