Brief reading of Java-API_java.util.Locale class (based on JDK1.8) (no source code involved)

[Copyright Statement] Without the consent of the blogger, please do not reprint! (Please respect the originality, the blogger reserves the right to pursue)
https://blog.csdn.net/m0_69908381/article/details/130603741
From【Progress* Yu Chen’s Blog】

In fact, the blog posts in my [Java-API] column are of little significance to everyone. Because there is no source code explanation for the time being, and you need to read the Java-API, you can just read the Java-API documentation directly, and you don’t need to read blog posts. So, this blog post is mainly written for myself, that is, Java-API notes.
Because I found that at present, my learning awareness of Java-API is relatively weak, and I need to gradually get used to using Java-API, and even analyze the source code to improve my source code reading ability and coding quality.
If you need Java-API documentation, I uploaded it【https://download.csdn.net/download/m0_69908381/87691693】.

Article directory

  • 1 Overview
  • 2. Field summary
    • 2.1 Useful constants representing countries
    • 2.2 Useful constants representing the language
  • 3. Summary of Construction Method
    • 3.1 String language
    • 3.2 String language, String country
    • 3.3 String language, String country, String variant
  • 4. Method summary
    • 4.1 Object clone()
    • 4.2 boolean equals(Object obj)
    • 4.3 static Locale[] getAvailableLocales()
    • 4.4 String getCountry()
    • 4.5 static Locale getDefault()
    • 4.6 String getDisplayCountry()
    • 4.7 String getDisplayCountry(Locale inLocale)
    • 4.8 String getDisplayLanguage()
    • 4.9 String getDisplayLanguage(Locale inLocale)
    • 4.10 String getDisplayName()
    • 4.11 String getDisplayName(Locale inLocale)
    • 4.12 String getDisplayVariant()
    • 4.13 String getDisplayVariant(Locale inLocale)
    • 4.14 String getISO3Country()
    • 4.15 String getISO3Language()
    • 4.16 static String[] getISOCountries()
    • 4.17 static String[] getISOLanguages()
    • 4.18 String getLanguage()
    • 4.19 String getVariant()
    • 4.20 int hashCode()
    • 4.21 static void setDefault(Locale newLocale)
    • 4.22 String to String()

1. Overview


A Locale object represents a specific geographic, political, and cultural locale. Operations that require a Locale to perform their tasks are called locale-sensitive operations, which use a Locale to tailor information to the user. For example, displaying a numeric value is locale-sensitive and should be formatted according to the customs/traditions of the user’s country, region, or culture.

Use the constructors in this class to create a Locale:

Locale(String language)
Locale(String language, String country)
Locale(String language, String country, String variant)

The Language Parameter is a valid ISO language code. These codes are lowercase two-letter codes as defined by ISO-639. A complete list of these codes can be found at a number of sites such as: http://www.loc.gov/standards/iso639-2/englangn.html.

Country Parameter is a valid ISO country code. These codes are uppercase two-letter codes as defined by ISO-3166. A complete list of these codes can be found on a number of websites, such as: http://www.iso.ch/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html.

[Variable parameter] is a code specific to vendor or browser. For example, use WIN for Windows, MAC for Macintosh, and POSIX for POSIX. When there are two variables, separate them with underscore, put the most important one first. For example, a traditional Spanish collation might construct a locale with the following language, country, and variable parameters: “es”, “ES”, “Traditional_WIN”.

Since a Locale object is just a locale identifier, no validity checking is performed when constructing a Locale. If you want to know whether particular resources are available in the Locale you constructed, you must query those resources. For example, use the getAvailableLocales method to ask the NumberFormat for supported locales.
Note: When querying for resources in a specific locale, the best available match is obtained, and it is not necessary to provide resources exactly as requested. See ResourceBundles for more information.

The Locale class provides some convenience constants that can be used to create Locale objects for commonly used locales. For example, the following creates a Locale object for the United States:

Locale.US

Once the Locale has been created, it can be queried for information about itself. Use getCountry() to get the ISO country code, use getLanguage() to get the ISO language code. You can use getDisplayCountry() to get the country name suitable for displaying to the user. Similarly, getDisplayLanguage() can be used to obtain the language name suitable for displaying to the user. Interestingly, the getDisplayXXX() method itself is locale-sensitive, and it has two versions: one takes the default locale as an argument, and the other uses the specified locale as an argument.

The Java 2 platform provides several classes that perform locale-sensitive operations: for example, the NumberFormat class formats numeric values, currency, or percentages in a locale-sensitive manner. Classes like NumberFormat have various convenience methods for creating default objects of that type. For example, the NumberFormat class provides 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 uses the default locale.

 NumberFormat. getInstance(myLocale)
 NumberFormat. getCurrencyInstance(myLocale)
 NumberFormat. getPercentInstance(myLocale)

Locale is a mechanism to identify the kind of object (NumberFormat) to get. But a locale is just a mechanism for identifying objects, not a container for the objects themselves.

Beginning with:
1.1
See also:
ResourceBundle, Format, NumberFormat, Collator,
serialized form

2. Field summary

2.1 Useful constants for countries

1. Country list:
CANADA, CANADA_FRENCH, CHINA, FRANCE, GERMANY, ITALIAN, ITALY, JAPAN, KOREA, PRC, TAIWAN, UK, US.

2.2 Indicates useful constants for the language

1. Language list:
CHINESE, ENGLISH, FRENCH, GERMAN, JAPANESE, KOREAN, SIMPLIFIED_CHINESE, TRADITIONAL_CHINESE.

3. Abstract of construction method

3.1 String language

Note: language → language code.
Example:

Locale de = Locale.CHINESE;
Locale newDe = new Locale(de. getLanguage());
sout newDe.getCountry() + "\t" + newDe.getDisplayCountry();// Note: Since [country] was not provided during construction, there is no result
sout newDe.getLanguage() + "\t" + newDe.getDisplayLanguage();// print: zh Chinese

3.2 String language, String country

Note: language → language code; country → country.
Example:

Locale de = Locale.CHINA;
Locale newDe = new Locale(de. getLanguage(), de. getCountry());
sout newDe.getCountry() + "\t" + newDe.getDisplayCountry();// print: CN China
sout newDe.getLanguage() + "\t" + newDe.getDisplayLanguage();// print: zh Chinese

3.3 String language, String country, String variant

Note: language → language code; country → country; variant → variable. (unknown yet)

4. Method summary

4.1 Object clone()

Override Cloneable.

4.2 boolean equals(Object obj)

Returns true if this Locale is equal to another object.
Example:

// Example 1:
Locale de = Locale. CHINESE;
Locale newDe = new Locale(de. getLanguage());
sout de.equals(newDe);// print: false

// Example 2:
Locale de = Locale.CHINA;
Locale newDe = new Locale(de. getLanguage(), de. getCountry());
sout de.equals(newDe);// print: true

4.3 static Locale[] getAvailableLocales()

Returns an array of all installed locales.
Example:

Locale[] lArr = Locale.getAvailableLocales();
int len = lArr. length;
while ((len--) > 0)
sout lArr[len];

4.4 String getCountry()

Returns the country code for this locale, which will be an empty string or an uppercase ISO 3166 two-letter code.

4.5 static Locale getDefault()

Gets the current default locale value for this Java virtual machine instance.

4.6 String getDisplayCountry()

Returns the locale country name suitable for displaying to the user.
Example:

Locale de = Locale.CHINA;
sout de.getDisplayCountry();// print: China

4.7 String getDisplayCountry(Locale inLocale)

Returns a locale country name suitable for display to the user. (unknown yet)

4.8 String getDisplayLanguage()

Returns the locale language name suitable for displaying to the user.
Example:

Locale de = Locale.CHINA;
sout de.getDisplayLanguage();// print: Chinese

4.9 String getDisplayLanguage(Locale inLocale)

Returns the locale language name suitable for display to the user. (unknown yet)

4.10 String getDisplayName()

Returns a locale name suitable for displaying to the user.
Example:

Locale de = Locale.CHINA;
sout de.getDisplayName();// print: Chinese (China)

4.11 String getDisplayName(Locale inLocale)

Returns a locale name suitable for display to the user. (unknown yet)

4.12 String getDisplayVariant()

Returns the locale variable code name suitable for displaying to the user. (unknown yet)

4.13 String getDisplayVariant(Locale inLocale)

Returns a locale variable code name suitable for display to the user. (unknown yet)

4.14 String getISO3Country()

Returns the 3-letter country abbreviation for this locale.
Example:

Locale de = Locale.CHINA;
sout de.getCountry() + "\t" + de.getISO3Country();// print: CN CHN

4.15 String getISO3Language()

Returns the three-letter abbreviation for the language of this locale.
Example:

Locale de = Locale.CHINA;
sout de.getLanguage() + "\t" + de.getISO3Language();// print: zh zo

4.16 static String[] getISOCountries()

Returns all two-letter country codes as defined in ISO 3166.

4.17 static String[] getISOLanguages()

Returns all two-letter language codes as defined in ISO 639.

4.18 String getLanguage()

Returns the language code for this locale, either an empty string or a lowercase ISO 639 code.

4.19 String getVariant()

Returns the variable code for this locale. (unknown yet)

4.20 int hashCode()

Override hashCode.

4.21 static void setDefault(Locale newLocale)

Sets the default locale for this Java virtual machine instance.
Example:

Locale l1 = Locale.UK;//United Kingdom
Locale. setDefault(l1);
Locale de = Locale. getDefault();
sout de.getCountry() + " " + de.getLanguage();// print: GB en

4.22 String toString()

Use language, country, and variables separated by underscores to get the programmatic name for the entire locale.

This article is temporarily being updated. . .