There are two ways to change the default
locale. The first is to set it on the command line:
> java -Duser.language=2-char-language-code -Duser.region=2-char-country-code MyApp
// Set only language code
> java -Duser.language=en -Duser.region= MyApp
// Set language and country code
> java -Duser.language=en -Duser.region=US MyApp
The second way to change the default locale is to call Locale.setDefault():
// Get default locale
Locale locale = Locale.getDefault();
// Set the default locale to pre-defined locale
Locale.setDefault(Locale.US);
// Set the default locale to custom locale
locale = new Locale("en", "US");
Locale.setDefault(locale);
> java -Duser.language=2-char-language-code -Duser.region=2-char-country-code MyApp
// Set only language code
> java -Duser.language=en -Duser.region= MyApp
// Set language and country code
> java -Duser.language=en -Duser.region=US MyApp
The second way to change the default locale is to call Locale.setDefault():
// Get default locale
Locale locale = Locale.getDefault();
// Set the default locale to pre-defined locale
Locale.setDefault(Locale.US);
// Set the default locale to custom locale
locale = new Locale("en", "US");
Locale.setDefault(locale);
本文介绍两种在Java中设置默认区域与语言的方法:一种是在命令行通过参数设定,另一种是通过调用Locale.setDefault()方法来更改默认设置。这两种方式能够帮助开发者灵活地为应用程序指定所需的默认区域和语言。
874

被折叠的 条评论
为什么被折叠?



