我有一个区域设置字符串java.util.Locale
。当我像这样创建一个新的String myLocaleStr = "fi_FI";
Locale locale = new Locale(myLocaleStr);
对象时 -
locale
- 并使用sysout
语句检查"fi_fi"
信息值,我发现显示的区域设置值为"fi_FI"
而不是{{1}}。
有什么方法可以保留区域设置值吗?
答案 0 :(得分:2)
您使用的Locale constructor认为您传递的唯一参数是语言。不是语言和国家的组合。拆分字符串,并使用the other constructor,接受语言和国家/地区:
extern
答案 1 :(得分:1)
我不确定Java的较新版本,但构造函数如下所示:
/**
* Construct a locale from language, country, variant.
* NOTE: ISO 639 is not a stable standard; some of the language codes it defines
* (specifically iw, ji, and in) have changed. This constructor accepts both the
* old codes (iw, ji, and in) and the new codes (he, yi, and id), but all other
* API on Locale will return only the OLD codes.
* @param language lowercase two-letter ISO-639 code.
* @param country uppercase two-letter ISO-3166 code.
* @param variant vendor and browser specific code. See class description.
* @exception NullPointerException thrown if any argument is null.
*/
public Locale(String language, String country, String variant) {
this.language = convertOldISOCodes(language);
this.country = toUpperCase(country).intern();
this.variant = variant.intern();
}
您是否看到国家/地区代码将设置为大写。这符合ISO标准。