java.util
Class Locale

java.lang.Object
  extended by java.util.Locale

public final class Locale
extends Object

Locales represent a specific country and culture. Classes which can be passed a Locale object tailor their information for a given locale. For instance, currency number formatting is handled differently for the USA and France.

Locales are made up of a language code, a country code, and an optional set of variant strings. Language codes are represented by ISO 639:1988 w/ additions from ISO 639/RA Newsletter No. 1/1989 and a decision of the Advisory Committee of ISO/TC39 on August 8, 1997.

Country codes are represented by ISO 3166. Variant strings are vendor and browser specific. Standard variant strings include "POSIX" for POSIX, "WIN" for MS-Windows, and "MAC" for Macintosh. When there is more than one variant string, they must be separated by an underscore (U+005F).

The default locale is determined by the values of the system properties user.language, user.region, and user.variant, defaulting to "en". Note that the locale does NOT contain the conversion and formatting capabilities (for that, use ResourceBundle and java.text). Rather, it is an immutable tag object for identifying a given locale, which is referenced by these other classes when they must make locale-dependent decisions.

Since:
1.1
See Also:
ResourceBundle, java.text.Format, java.text.NumberFormat, java.text.Collator

Constructor Summary
Locale(String language)
          Creates a new locale for a language.
Locale(String language, String country)
          Creates a new locale for the given language and country.
Locale(String language, String country, String variant)
          Creates a new locale for the given language and country.
 
Method Summary
 boolean equals(Object obj)
          Compares two locales.
 String getCountry()
          Returns the country code of this locale.
static Locale getDefault()
          Returns the default Locale.
 String getLanguage()
          Returns the language code of this locale.
 String getVariant()
          Returns the variant code of this locale.
 int hashCode()
          Return the hash code for this locale.
 String toString()
          Gets the string representation of the current locale.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Locale

public Locale(String language,
              String country,
              String variant)
Creates a new locale for the given language and country.

Parameters:
language - lowercase two-letter ISO-639 A2 language code
country - uppercase two-letter ISO-3166 A2 contry code
variant - vendor and browser specific
Throws:
NullPointerException - if any argument is null

Locale

public Locale(String language,
              String country)
Creates a new locale for the given language and country.

Parameters:
language - lowercase two-letter ISO-639 A2 language code
country - uppercase two-letter ISO-3166 A2 country code
Throws:
NullPointerException - if either argument is null

Locale

public Locale(String language)
Creates a new locale for a language.

Parameters:
language - lowercase two-letter ISO-639 A2 language code
Throws:
NullPointerException - if either argument is null
Since:
1.4
Method Detail

getDefault

public static Locale getDefault()
Returns the default Locale. The default locale is generally once set on start up and then never changed. Normally you should use this locale for everywhere you need a locale. The initial setting matches the default locale, the user has chosen.

Returns:
the default locale for this virtual machine

getLanguage

public String getLanguage()
Returns the language code of this locale. Some language codes have changed as ISO 639 has evolved; this returns the old name, even if you built the locale with the new one.

Returns:
language code portion of this locale, or an empty String

getCountry

public String getCountry()
Returns the country code of this locale.

Returns:
country code portion of this locale, or an empty String

getVariant

public String getVariant()
Returns the variant code of this locale.

Returns:
the variant code portion of this locale, or an empty String

toString

public String toString()
Gets the string representation of the current locale. This consists of the language, the country, and the variant, separated by an underscore. The variant is listed only if there is a language or country. Examples: "en", "de_DE", "_GB", "en_US_WIN", "de__POSIX", "fr__MAC".

Overrides:
toString in class Object
Returns:
the string representation of this Locale
See Also:
#getDisplayName()

hashCode

public int hashCode()
Return the hash code for this locale. The hashcode is the logical xor of the hash codes of the language, the country and the variant. The hash code is precomputed, since Locales are often used in hash tables.

Overrides:
hashCode in class Object
Returns:
the hashcode
See Also:
Object.equals(Object), System.identityHashCode(Object)

equals

public boolean equals(Object obj)
Compares two locales. To be equal, obj must be a Locale with the same language, country, and variant code.

Overrides:
equals in class Object
Parameters:
obj - the other locale
Returns:
true if obj is equal to this
See Also:
Object.hashCode()