Java Locale getDisplayCountry() 方法



描述

Java Locale getDisplayCountry() 方法返回適合顯示給使用者的區域設定國家/地區的名稱。

宣告

以下是 java.util.Locale.getDisplayCountry() 方法的宣告

public final String getDisplayCountry()

引數

返回值

此方法返回適合該區域設定的國家/地區的名稱。

異常

Java Locale getDisplayCountry(Locale inLocale) 方法

描述

Java Locale getDisplayCountry(Locale inLocale) 方法返回適合顯示給使用者的區域設定國家/地區的名稱。如果可能,返回的名稱將根據 inLocale 進行本地化。

宣告

以下是 java.util.Locale.getDisplayCountry() 方法的宣告

public String getDisplayCountry(Locale inLocale)

引數

返回值

此方法返回適合給定區域設定的國家/地區的名稱。

異常

NullPointerException - 如果 inLocale 為 null

獲取美國區域設定的顯示國家/地區示例

以下示例顯示了 Java Locale getDisplayCountry() 方法的使用。我們正在建立一個美國區域設定,然後檢索並列印其國家/地區。

package com.tutorialspoint;

import java.util.Locale;

public class LocaleDemo {
   public static void main(String[] args) {

      // create a new locale
      Locale locale = Locale.US;

      // print this locale
      System.out.println("Locale1:" + locale);

      // print the country of this locale
      System.out.println("Country:" + locale.getDisplayCountry());
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Locale1:en_US
Country:United States

獲取加拿大區域設定的顯示國家/地區示例

以下示例顯示了 Java Locale getDisplayCountry() 方法的使用。我們正在建立一個加拿大區域設定,然後檢索並列印其國家/地區。

package com.tutorialspoint;

import java.util.Locale;

public class LocaleDemo {
   public static void main(String[] args) {

      // create a new locale
      Locale locale = Locale.CANADA;

      // print this locale
      System.out.println("Locale1:" + locale);

      // print the country of this locale
      System.out.println("Country:" + locale.getDisplayCountry());
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Locale1:en_CA
Country:Canada

獲取法國區域設定的顯示國家/地區示例

以下示例顯示了 Java Locale getDisplayCountry(Locale) 方法的使用。我們正在建立一個法國區域設定,然後使用德語區域設定檢索並列印其國家/地區。

package com.tutorialspoint;

import java.util.Locale;

public class LocaleDemo {
   public static void main(String[] args) {

      // create a new locale
      Locale locale = Locale.FRANCE;

      // print this locale
      System.out.println("Locale1:" + locale);

      // print the country of this locale
	  System.out.println("Country:" + locale.getDisplayCountry());
      System.out.println("Country:" + locale.getDisplayCountry(Locale.GERMANY));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Locale1:fr_FR
Country:France
Country:Frankreich
java_util_locale.htm
廣告