Java Locale getCountry() Method



Description

The Java Locale getCountry() method returns the country/region code for this locale, which will either be the empty string or an uppercase ISO 3166 2-letter code.

Declaration

Following is the declaration for java.util.Locale.getCountry() method

public String getCountry()

Parameters

NA

Return Value

This method does not return a value.

Exception

NA

Getting Country from US Locale Example

The following example shows the usage of Java Locale getCountry() method. We're creating a locale of US and then its country is retrieved and printed.

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

Output

Let us compile and run the above program, this will produce the following result −

Locale1:en_US
Country:US

Getting Country from Canada Locale Example

The following example shows the usage of Java Locale getCountry() method. We're creating a locale of Canada and then its country is retrieved and printed.

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

Output

Let us compile and run the above program, this will produce the following result −

Locale1:en_CA
Country:CA

Getting Country from FRANCE Locale Example

The following example shows the usage of Java Locale getCountry() method. We're creating a locale of France and then its country is retrieved and printed.

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

Output

Let us compile and run the above program, this will produce the following result −

Locale1:fr_FR
Country:FR
java_util_locale.htm
Advertisements