DateFormat.get*Instance()
에 의해 반환 된 객체를 SimpleDateFormat
으로 캐스팅하지 않고도 지정된 로케일의 기본 패턴을 검색 할 수 있습니까?주어진 로케일의 기본 패턴을 검색하는 또 다른 방법이 있습니까?
는 그 대부분의 경우 모든 것이 확인 될 것입니다, 이해하지만 메모는 javadoc
에 here있다 : "당신이 (또는 사용자에게 더 많은 제어를 제공 할) 형식 또는 구문 분석, 한층 더 제어하려면, 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;
}
패턴 기반이 아니기 때문에 "이상한 것이 발생했습니다"라고 말하면 무엇이든 할 수 있는지 확실하지 않습니다. 특히 개발자가 java.text.spi.DateFormatProvider를 통해 자신의'DateFormat'을 제공 할 수 있기 때문에 특히 그렇습니다. –