6

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;
}
Community
  • 1
  • 1
Milkywayfarer
  • 910
  • 1
  • 9
  • 25
  • I'm not sure if you can do anything if you "encounter an unusual one" because it may not be pattern-based. Especially because a developer may provide their own `DateFormat` via `java.text.spi.DateFormatProvider`. – The Alchemist May 04 '11 at 14:36

2 Answers2

2

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.

Speck
  • 2,259
  • 1
  • 20
  • 29
  • Thx for your answer. Although you didn't provide direct answer for concrete question, you suggested harmonious solution to handle this possible situation (as shown by practice very not likely situation, but however possible). So I think your answer could be accepted as solution. – Milkywayfarer May 08 '11 at 11:56
  • More over thx for your comments to the code. Answering to your questions: – Milkywayfarer May 08 '11 at 11:59
  • 1) yes, I agree that using collection of strings would be more convenient, but this was just demonstrative code to clarify what was question about. – Milkywayfarer May 08 '11 at 12:00
  • 2) of course StringBuilder should be use in preference to String in such a case – Milkywayfarer May 08 '11 at 12:01
1

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.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111