[all packages] [package java.util] [class hierarchy] [index]

public final class java.util.Locale

(source file: Locale.java)
java.lang.Object
   |
   +----java.util.Locale

The pure class interface.
public final class Locale
  implements Cloneable, Serializable
A Locale object represents a specific geographical, political, or cultural region. An operation that requires a Locale to perform its task is called locale-sensitive and uses the Locale to tailor information for the user. For example, displaying a number is a locale-sensitive operation--the number should be formatted according to the customs/conventions of the user's native country, region, or culture.

You create a Locale object using one of the two constructors in this class:

 Locale(String language, String country)
 Locale(String language, String country, String variant)
 
The first argument to both constructors is a valid ISO Language Code. These codes are the lower-case two-letter codes as defined by ISO-639. You can find a full list of these codes at a number of sites, such as:
http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt

The second argument to both constructors is a valid ISO Country Code. These codes are the upper-case two-letter codes as defined by ISO-3166. You can find a full list of these codes at a number of sites, such as:
http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html

The second constructor requires a third argument--the Variant. The Variant codes are vendor and browser-specific. For example, use WIN for Windows, MAC for Macintosh, and POSIX for POSIX. Where there are two variants, separate them with an underscore, and put the most important one first. For example, a Traditional Spanish collation might construct a locale with parameters for language, country and variant as: "es", "ES", "Traditional_WIN".

Because a Locale object is just an identifier for a region, no validity check is performed when you construct a Locale. If you want to see whether particular resources are available for the Locale you construct, you must query those resources. For example, ask the NumberFormat for the locales it supports using its getAvailableLocales method.
Note: When you ask for a resource for a particular locale, you get back the best available match, not necessarily precisely what you asked for. For more information, look at ResourceBundle.

The Locale class provides a number of convenient constants that you can use to create Locale objects for commonly used locales. For example, the following creates a Locale object for the United States:

 Locale.US
 

Once you've created a Locale you can query it for information about itself. Use getCountry to get the ISO Country Code and getLanguage to get the ISO Language Code. You can use getDisplayCountry to get the name of the country suitable for displaying to the user. Similarly, you can use getDisplayLanguage to get the name of the language suitable for displaying to the user. Interestingly, the getDisplayXXX methods are themselves locale-sensitive and have two versions: one that uses the default locale and one that uses the locale specified as an argument.

The JDK provides a number of classes that perform locale-sensitive operations. For example, the NumberFormat class formats numbers, currency, or percentages in a locale-sensitive manner. Classes such as NumberFormat have a number of convenience methods for creating a default object of that type. For example, the NumberFormat class provides these three convenience methods for creating a default NumberFormat object:

 NumberFormat.getInstance()
 NumberFormat.getCurrencyInstance()
 NumberFormat.getPercentInstance()
 
These methods have two variants; one with an explicit locale and one without; the latter using the default locale.
 NumberFormat.getInstance(myLocale)
 NumberFormat.getCurrencyInstance(myLocale)
 NumberFormat.getPercentInstance(myLocale)
 
A Locale is the mechanism for identifying the kind of object (NumberFormat) that you would like to get. The locale is just a mechanism for identifying objects, not a container for the objects themselves.

Each class that performs locale-sensitive operations allows you to get all the available objects of that type. You can sift through these objects by language, country, or variant, and use the display names to present a menu to the user. For example, you can create a menu of all the collation objects suitable for a given language. Such classes must implement these three class methods:

 public static Locale[] getAvailableLocales()
 public static String getDisplayName(Locale objectLocale,
                                     Locale displayLocale)
 public static final String getDisplayName(Locale objectLocale)
     // getDisplayName will throw MissingResourceException if the locale
     // is not one of the available locales.
 

See also:
ResourceBundle, java.text.Format, java.text.NumberFormat, java.text.Collation

Constuctor Index

O Locale(String, String)
Construct a locale from language, country.
O Locale(String, String, String)
Construct a locale from language, country, variant.

Variables Index

O CANADA
Useful constant for country.
O CANADA_FRENCH
Useful constant for country.
O CHINA
Useful constant for country.
O CHINESE
Useful constant for language.
O ENGLISH
Useful constant for language.
O FRANCE
Useful constant for country.
O FRENCH
Useful constant for language.
O GERMAN
Useful constant for language.
O GERMANY
Useful constant for country.
O ITALIAN
Useful constant for language.
O ITALY
Useful constant for country.
O JAPAN
Useful constant for country.
O JAPANESE
Useful constant for language.
O KOREA
Useful constant for country.
O KOREAN
Useful constant for language.
O PRC
Useful constant for country.
O SIMPLIFIED_CHINESE
Useful constant for language.
O TAIWAN
Useful constant for country.
O TRADITIONAL_CHINESE
Useful constant for language.
O UK
Useful constant for country.
O US
Useful constant for country.

Methods

O clone()
Overrides Cloneable
O equals(Object)
O getAvailableLocales()
Returns a list of all installed locales.
O getCountry()
Getter for programmatic name of field, an uppercased two-letter ISO-3166 code.
O getDefault()
Common method of getting the current default Locale. Used for the presentation: menus,
O getDisplayCountry(Locale)
Returns a name for the locale's country that is appropriate for display to the user
O getDisplayCountry()
Returns a name for the locale's country that is appropriate for display to the user
O getDisplayLanguage(Locale)
Returns a name for the locale's language that is appropriate for display to the user
O getDisplayLanguage()
Returns a name for the locale's language that is appropriate for display to the user
O getDisplayName(Locale)
Returns a name for the locale that is appropriate for display to the user
O getDisplayName()
Returns a name for the locale that is appropriate for display to the user
O getDisplayVariant(Locale)
Returns a name for the locale's variant code that is appropriate for display to the user
O getDisplayVariant()
Returns a name for the locale's variant code that is appropriate for display to the user
O getISO3Country()
Getter for the three-letter ISO country abbreviation of the locale
O getISO3Language()
Getter for the three-letter ISO language abbreviation of the locale
O getISOCountries()
Returns a list of all 2-letter country codes defined in ISO 3166. Can be used to create
O getISOLanguages()
Returns a list of all 2-letter language codes defined in ISO 639. Can be used to create
O getLanguage()
Getter for programmatic name of field, an lowercased two-letter ISO-639 code.
O getVariant()
Getter for programmatic name of field.
O hashCode()
Override hashCode. Since Locales are often used in hashtables, caches the value for speed.
O setDefault(Locale)
Sets the default. Normally set once at the beginning of applet or application, then never
O toString()
Getter for the programmatic name of the entire locale, with the language, country and

Constructors

O Locale
public Locale(String language,
              String country,
              String variant);
Construct a locale from language, country, variant.

Parameters:
language - lowercase two-letter ISO-639 code.
country - uppercase two-letter ISO-3166 code.
variant - vendor and browser specific code. See class description.

O Locale

public Locale(String language,
              String country);
Construct a locale from language, country.

Parameters:
language - lowercase two-letter ISO-639 code.
country - uppercase two-letter ISO-3166 code.

Variables

O ENGLISH
public static final Locale ENGLISH;
Useful constant for language.

O FRENCH

public static final Locale FRENCH;
Useful constant for language.

O GERMAN

public static final Locale GERMAN;
Useful constant for language.

O ITALIAN

public static final Locale ITALIAN;
Useful constant for language.

O JAPANESE

public static final Locale JAPANESE;
Useful constant for language.

O KOREAN

public static final Locale KOREAN;
Useful constant for language.

O CHINESE

public static final Locale CHINESE;
Useful constant for language.

O SIMPLIFIED_CHINESE

public static final Locale SIMPLIFIED_CHINESE;
Useful constant for language.

O TRADITIONAL_CHINESE

public static final Locale TRADITIONAL_CHINESE;
Useful constant for language.

O FRANCE

public static final Locale FRANCE;
Useful constant for country.

O GERMANY

public static final Locale GERMANY;
Useful constant for country.

O ITALY

public static final Locale ITALY;
Useful constant for country.

O JAPAN

public static final Locale JAPAN;
Useful constant for country.

O KOREA

public static final Locale KOREA;
Useful constant for country.

O CHINA

public static final Locale CHINA;
Useful constant for country.

O PRC

public static final Locale PRC;
Useful constant for country.

O TAIWAN

public static final Locale TAIWAN;
Useful constant for country.

O UK

public static final Locale UK;
Useful constant for country.

O US

public static final Locale US;
Useful constant for country.

O CANADA

public static final Locale CANADA;
Useful constant for country.

O CANADA_FRENCH

public static final Locale CANADA_FRENCH;
Useful constant for country.

Methods

O getDefault
public static Locale getDefault();
Common method of getting the current default Locale. Used for the presentation: menus, dialogs, etc. Generally set once when your applet or application is initialized, then never reset. (If you do reset the default locale, you probably want to reload your GUI, so that the change is reflected in your interface.)

More advanced programs will allow users to use different locales for different fields, e.g. in a spreadsheet.
Note that the initial setting will match the host system.

O setDefault

public static synchronized void setDefault(Locale newLocale);
Sets the default. Normally set once at the beginning of applet or application, then never reset. setDefault does not reset the host locale.

Parameters:
newLocale - Locale to set to.

O getAvailableLocales

public static Locale[] getAvailableLocales();
Returns a list of all installed locales.

O getISOCountries

public static String[] getISOCountries();
Returns a list of all 2-letter country codes defined in ISO 3166. Can be used to create Locales.

O getISOLanguages

public static String[] getISOLanguages();
Returns a list of all 2-letter language codes defined in ISO 639. Can be used to create Locales.

O getLanguage

public String getLanguage();
Getter for programmatic name of field, an lowercased two-letter ISO-639 code.

See also:
getDisplayLanguage

O getCountry

public String getCountry();
Getter for programmatic name of field, an uppercased two-letter ISO-3166 code.

See also:
getDisplayCountry

O getVariant

public String getVariant();
Getter for programmatic name of field.

See also:
getDisplayVariant

O toString

public final String toString();
Getter for the programmatic name of the entire locale, with the language, country and variant separated by underbars. Language is always lower case, and country is always uppcer case. If a field is missing, at most one underbar will occur. Example: "en", "de_DE", "en_US_WIN", "de_POSIX", "fr_MAC"

Overrides:
toString in class Object
See also:
getDisplayName

O getISO3Language

public String getISO3Language()
  throws MissingResourceException;
Getter for the three-letter ISO language abbreviation of the locale. Returns the empty string if the locale doesn't specify a language.

Throws:
MissingResourceException -Throws MissingResourceException if the three-letter language abbreviation is not available for this locale.

O getISO3Country

public String getISO3Country()
  throws MissingResourceException;
Getter for the three-letter ISO country abbreviation of the locale. Returns the empty string if the locale doesn't specify a country.

Throws:
MissingResourceException -Throws MissingResourceException if the three-letter language abbreviation is not available for this locale.

O getDisplayLanguage

public final String getDisplayLanguage();
Returns a name for the locale's language that is appropriate for display to the user. This will be the name the locale's language localized for the default locale, if that data is available. For example, if the locale is fr_FR and the default locale is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and the default locale is fr_FR, getDisplayLanguage() will return "anglais". If the appropriate name isn't available (say, we don't have a Japanese name for Croatian), this function falls back on the English name and uses the ISO code as a last-resort value. If the locale doesn't specify a language, this function returns the empty string.

O getDisplayLanguage

public String getDisplayLanguage(Locale inLocale);
Returns a name for the locale's language that is appropriate for display to the user. This will be the name the locale's language localized for inLocale, if that data is available. For example, if the locale is fr_FR and inLocale is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and inLocale is fr_FR, getDisplayLanguage() will return "anglais". If the appropriate name isn't available (say, we don't have a Japanese name for Croatian), this function falls back on the default locale, on the English name, and finally on the ISO code as a last-resort value. If the locale doesn't specify a language, this function returns the empty string.

O getDisplayCountry

public final String getDisplayCountry();
Returns a name for the locale's country that is appropriate for display to the user. This will be the name the locale's country localized for the default locale, if that data is available. For example, if the locale is fr_FR and the default locale is en_US, getDisplayCountry() will return "France"; if the locale is en_US and the default locale is fr_FR, getDisplayLanguage() will return "Etats-Unis". If the appropriate name isn't available (say, we don't have a Japanese name for Croatia), this function falls back on the English name and uses the ISO code as a last-resort value. If the locale doesn't specify a country, this function returns the empty string.

O getDisplayCountry

public String getDisplayCountry(Locale inLocale);
Returns a name for the locale's country that is appropriate for display to the user. This will be the name the locale's country localized for inLocale, if that data is available. For example, if the locale is fr_FR and inLocale is en_US, getDisplayCountry() will return "France"; if the locale is en_US and inLocale is fr_FR, getDisplayLanguage() will return "Etats-Unis". If the appropriate name isn't available (say, we don't have a Japanese name for Croatia), this function falls back on the default locale, on the English name, and finally on the ISO code as a last-resort value. If the locale doesn't specify a country, this function returns the empty string.

O getDisplayVariant

public final String getDisplayVariant();
Returns a name for the locale's variant code that is appropriate for display to the user. If possible, the name will be localized for the default locale. If the locale doesn't specify a variant code, this function returns the empty string.

O getDisplayVariant

public String getDisplayVariant(Locale inLocale);
Returns a name for the locale's variant code that is appropriate for display to the user. If possible, the name will be localized for inLocale. If the locale doesn't specify a variant code, this function returns the empty string.

O getDisplayName

public final String getDisplayName();
Returns a name for the locale that is appropriate for display to the user. This will be the values returned by getDisplayLanguage(), getDisplayCountry(), and getDisplayVariant() assembled into a single string. The display name will have one of the following forms:

language (country, variant)

language (country)

language (variant)

country (variant)

language

country

variant

depending on which fields are specified in the locale. If the language, country, and variant fields are all empty, this function returns the empty string.

O getDisplayName

public String getDisplayName(Locale inLocale);
Returns a name for the locale that is appropriate for display to the user. This will be the values returned by getDisplayLanguage(), getDisplayCountry(), and getDisplayVariant() assembled into a single string. The display name will have one of the following forms:

language (country, variant)

language (country)

language (variant)

country (variant)

language

country

variant

depending on which fields are specified in the locale. If the language, country, and variant fields are all empty, this function returns the empty string.

O clone

public Object clone();
Overrides Cloneable

Overrides:
clone in class Object

O hashCode

public synchronized int hashCode();
Override hashCode. Since Locales are often used in hashtables, caches the value for speed.

Overrides:
hashCode in class Object

O equals

public boolean equals(Object obj);


[all packages] [package java.util] [class hierarchy] [index]
java.util.Locale.html