是否有另一种方法来检索给定语言环境的默认模式? [英] Is there another way to retrieve default pattern for a given locale?

查看:140
本文介绍了是否有另一种方法来检索给定语言环境的默认模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以检索给定语言环境的默认模式,而不将 DateFormat.get * Instance()返回的对象转换为 SimpleDateFormat

Is it possible to retrieve a default pattern for a given locale, without casting an object returned by DateFormat.get*Instance() to a SimpleDateFormat?

我明白,在大多数情况下,一切都会好的,但 javadoc 此处:如果你想要更多地控制格式或解析,(或者想让你的用户获得更多控制权),你可以尝试转换你从中得到的 DateFormat 工厂方法为 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"?

Rel主题。

代码示例:

/**
 * 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代码的特定语言的符号:

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.

DateFormatSymbols 类至少存在于Java 1.4.2之后。

The DateFormatSymbols class has existed since at least Java 1.4.2.

这篇关于是否有另一种方法来检索给定语言环境的默认模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆