当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Locale getISO3Country()用法及代码示例


Java中的Locale类的getISO3Country()方法用于获取指定语言环境的国家或地区代码。这将是大写的ISO 3166 3字母国家/地区代码,如果语言环境未指定国家/地区,则为空字符串。

用法:

LOCALE.getISO3Country()

参数:此方法不带任何参数。


返回值:此方法不返回任何值。

异常:如果three-letter国家/地区缩写不适用于指定的语言环境,则该方法将引发MissingResourceException。

以下示例程序旨在说明getISO3Country()方法的用法:

示例1:

// 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

示例2:

// 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


相关用法


注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 Locale getISO3Country() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。