Menu
Topics Index
...
`


More Utility Classes >
Siva Nookala - 15 Apr 2016
The Locale class is instantiated to produce objects that describe a geographical or cultural region. It is one of several classes that provide us with the ability to write programs that can execute in different international environments. For example, the formats used to display dates, times, and numbers are different in various regions.

The Locale class defines the following constants that are useful for dealing with several common locales:
CANADAGERMANKOREAN
CANADA_FRENCHGERMANYPRC
CHINAITALIANSIMPLIFIED_CHINESE
CHINESEITALYTAIWAN
ENGLISHJAPANTRADITIONAL_CHINESE
FRANCEJAPANESEUK
FRENCHKOREAUS

For example, the expression Locale.CANADA represents the Locale object for Canada.

The constructors for Locale are :
ConstructorDescription
Locale(String language) This constructs a locale from a language code.
Locale(String language, String country) This constructs a locale from a language code.

Locale defines several methods some of the most important are shown below :
Method Description
void setDefault(Locale localeObj) This sets the default locale used by the JVM to that specified by localeObj.
static Locale getDefault() This method gets the current value of the default locale for this instance of the Java Virtual Machine.
String getDisplayCountry() This method returns a name for the locale's country that is appropriate for display to the user.
String getDisplayLanguage() This method returns a name for the locale's language that is appropriate for display to the user.
String getDisplayName() This method returns a name for the locale that is appropriate for display to the user.
String getLanguage() This method returns the language code for this locale, which will either be the empty string or a lowercase ISO 639 code.
LocaleDemo
import java.util.Locale;

class LocaleDemo
{
    public static void main(String[] args)
    {
        Locale l = Locale.getDefault(); // LINE A
        System.out.println(l);
        System.out.println(l.getDisplayCountry()); // LINE B
        System.out.println(l.getCountry()); // LINE C
        System.out.println(l.getDisplayLanguage()); // LINE D
        System.out.println(l.getLanguage()); // LINE E
    }
}
OUTPUT

en_US
United States
US
English
en

DESCRIPTION

In the above program at LINE A we are getting the current value of the default locale of the Java Virtual Machine. At LINE B , LINE Cwe are printing the appropriate country name and country code. At LINE D, LINE E we are printing country language and language code.

THINGS TO TRY
  • Set the default locale as CANADA
  • Display the country name and language

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App