Java的Locale getISO3Country() 方法和示例
Java中Locale 类的getISO3Country()方法用于获取指定语言环境的国家或地区代码。如果语言环境未指定国家/地区,则这将是大写的 ISO 3166 3 字母国家/地区代码或空字符串。
句法:
LOCALE.getISO3Country()
参数:该方法不带任何参数。
返回值:此方法不返回任何值。
例外:如果三个字母的国家/地区缩写不适用于指定的语言环境,则该方法将引发MissingResourceException 。
下面的程序说明了 getISO3Country() 方法的工作:
方案一:
// Java code to illustrate getISO3Country() method
import java.util.*;
public class Locale_Demo {
public static void main(String[] args)
{
// Creating a new locale
Locale first_locale
= new Locale("en", "IN");
// Displaying first locale
System.out.println("First Locale: "
+ first_locale);
// Displaying the country_code of this locale
System.out.println("Country: "
+ first_locale.getISO3Country());
}
}
输出:
First Locale: en_IN
Country: IND
方案二:
// Java code to illustrate getISO3Country() method
import java.util.*;
public class Locale_Demo {
public static void main(String[] args)
{
// Creating a new locale
Locale first_locale
= new Locale("ar", "SA");
// Displaying first locale
System.out.println("First Locale: "
+ first_locale);
// Displaying the country_code of this locale
System.out.println("Country: "
+ first_locale.getISO3Country());
}
}
输出:
First Locale: ar_SA
Country: SAU
程序 3:显示错误
// Java code to illustrate getISO3Country() method
import java.util.*;
public class Locale_Demo {
public static void main(String[] args)
{
// Creating a new locale
Locale first_locale
= new Locale("ar", "tez");
// Displaying first locale
System.out.println("First Locale: "
+ first_locale);
try {
// Displaying the country_code of this locale
System.out.println("Country: "
+ first_locale.getISO3Country());
}
catch (Exception e) {
System.out.println(e);
}
}
}
输出:
First Locale: ar_TEZ
java.util.MissingResourceException: Couldn't find 3-letter country code for TEZ