All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class java.util.Locale

java.lang.Object
   |
   +----java.util.Locale

public final class Locale
extends Object
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 be referenced, with "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, Format, NumberFormat, Collation

Variable 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.

Constructor Index

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

Method Index

 o clone()
Overrides Cloneable
 o equals(Object)
Compares two Objects for equality.
 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.
 o getDisplayCountry()
Getter for display of field to user.
 o getDisplayCountry(Locale)
Getter for display of field to user.
 o getDisplayLanguage()
Getter for display of field to user.
 o getDisplayLanguage(Locale)
Getter for display of field to user.
 o getDisplayName()
Getter for display of the entire locale to user.
 o getDisplayName(Locale)
Getter for display of the entire locale to user.
 o getDisplayVariant()
Getter for display of field to user.
 o getDisplayVariant(Locale)
Getter for display of field to user If the localized name is not found, returns the variant code.
 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 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.
 o setDefault(Locale)
Sets the default.
 o toString()
Getter for the programmatic name of the entire locale, with the language, country and variant separated by underbars.

Variables

 o ENGLISH
Properties Public Constant 
    ENGLISH=Locale 
Useful constant for language.

 o FRENCH
Properties Public Constant 
    FRENCH=Locale 
Useful constant for language.

 o GERMAN
Properties Public Constant 
    GERMAN=Locale 
Useful constant for language.

 o ITALIAN
Properties Public Constant 
    ITALIAN=Locale 
Useful constant for language.

 o JAPANESE
Properties Public Constant 
    JAPANESE=Locale 
Useful constant for language.

 o KOREAN
Properties Public Constant 
    KOREAN=Locale 
Useful constant for language.

 o CHINESE
Properties Public Constant 
    CHINESE=Locale 
Useful constant for language.

 o SIMPLIFIED_CHINESE
Properties Public Constant 
    SIMPLIFIED_CHINESE=Locale 
Useful constant for language.

 o TRADITIONAL_CHINESE
Properties Public Constant 
    TRADITIONAL_CHINESE=Locale 
Useful constant for language.

 o FRANCE
Properties Public Constant 
    FRANCE=Locale 
Useful constant for country.

 o GERMANY
Properties Public Constant 
    GERMANY=Locale 
Useful constant for country.

 o ITALY
Properties Public Constant 
    ITALY=Locale 
Useful constant for country.

 o JAPAN
Properties Public Constant 
    JAPAN=Locale 
Useful constant for country.

 o KOREA
Properties Public Constant 
    KOREA=Locale 
Useful constant for country.

 o CHINA
Properties Public Constant 
    CHINA=Locale 
Useful constant for country.

 o PRC
Properties Public Constant 
    PRC=Locale 
Useful constant for country.

 o TAIWAN
Properties Public Constant 
    TAIWAN=Locale 
Useful constant for country.

 o UK
Properties Public Constant 
    UK=Locale 
Useful constant for country.

 o US
Properties Public Constant 
    US=Locale 
Useful constant for country.

 o CANADA
Properties Public Constant 
    CANADA=Locale 
Useful constant for country.

 o CANADA_FRENCH
Properties Public Constant 
    CANADA_FRENCH=Locale 
Useful constant for country.

Constructors

 o Locale
    method Locale(language=String, -
                  country=String, -
                  variant=String) public 
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
    method Locale(language=String, -
                  country=String) public 
Construct a locale from language, country.

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

Methods

 o getDefault
    method getDefault() returns Locale public  static  protect 
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
    method setDefault(newLocale=Locale)  public  static  protect 
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 getLanguage
    method getLanguage() returns String public     
Getter for programmatic name of field, an lowercased two-letter ISO-639 code.

See Also:
getDisplayLanguage
 o getCountry
    method getCountry() returns String public     
Getter for programmatic name of field, an uppercased two-letter ISO-3166 code.

See Also:
getDisplayCountry
 o getVariant
    method getVariant() returns String public     
Getter for programmatic name of field.

See Also:
getDisplayVariant
 o toString
    method toString() returns String public   final  
Getter for the programmatic name of the entire locale, with the language, country and variant separated by underbars. 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
    method getISO3Language() returns String public     Signals MissingResourceException
Getter for the three-letter ISO language abbreviation of the locale.

Throws: MissingResourceException
Throws MissingResourceException if the three-letter language abbreviation is not available for this locale.
 o getISO3Country
    method getISO3Country() returns String public     Signals MissingResourceException
Getter for the three-letter ISO country abbreviation of the locale.

Throws: MissingResourceException
Throws MissingResourceException if the three-letter language abbreviation is not available for this locale.
 o getDisplayLanguage
    method getDisplayLanguage() returns String public   final  
Getter for display of field to user. If the localized name is not found, returns the ISO code. The desired user language is from the default locale.

 o getDisplayLanguage
    method getDisplayLanguage(inLocale=Locale) returns String public     
Getter for display of field to user. If the localized name is not found, returns the ISO codes. Example: "English (UK)", "Deutch", "Germany"

Parameters:
inLocale - specifies the desired user language.
 o getDisplayCountry
    method getDisplayCountry() returns String public   final  
Getter for display of field to user. If the localized name is not found, returns the ISO code. The default locale is used for the presentation language.

 o getDisplayCountry
    method getDisplayCountry(inLocale=Locale) returns String public     
Getter for display of field to user. If the localized name is not found, returns the ISO code.

Parameters:
inLocale - specifies the desired user language.
 o getDisplayVariant
    method getDisplayVariant() returns String public   final  
Getter for display of field to user. If the localized name is not found, returns the variant code. The default locale is used for the presentation language.

 o getDisplayVariant
    method getDisplayVariant(inLocale=Locale) returns String public     
Getter for display of field to user If the localized name is not found, returns the variant code.

Parameters:
inLocale - specifies the desired user language.
 o getDisplayName
    method getDisplayName() returns String public   final  
Getter for display of the entire locale to user. If the localized name is not found, uses the ISO codes. The default locale is used for the presentation language.

 o getDisplayName
    method getDisplayName(inLocale=Locale) returns String public     
Getter for display of the entire locale to user. If the localized name is not found, uses the ISO codes

Parameters:
inLocale - specifies the desired user language.
 o clone
    method clone() returns Object public     
Overrides Cloneable

Overrides:
clone in class Object
 o hashCode
    method hashCode() returns int public    protect 
Override hashCode. Since Locales are often used in hashtables, caches the value for speed.

Overrides:
hashCode in class Object
 o equals
    method equals(obj=Object) returns boolean public     
Compares two Objects for equality.

Overrides:
equals in class Object

All Packages  Class Hierarchy  This Package  Previous  Next  Index