Java Locale getCountry() 方法



描述

Java Locale getCountry() 方法返回此區域設定的國家/地區程式碼,該程式碼將為空字串或大寫的 ISO 3166 2 字母程式碼。

宣告

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

public String getCountry()

引數

返回值

此方法不返回值。

異常

從 US 區域設定獲取國家示例

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

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.getCountry());
   }
}

輸出

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

Locale1:en_US
Country:US

從 Canada 區域設定獲取國家示例

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

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.getCountry());
   }
}

輸出

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

Locale1:en_CA
Country:CA

從 FRANCE 區域設定獲取國家示例

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

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.getCountry());
   }
}

輸出

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

Locale1:fr_FR
Country:FR
java_util_locale.htm
廣告