Java locale 類別是 java.util 套件中的最後一個類,有助於根據使用者的地理區域呈現資訊。需要顯示的資訊根據特定的地理、政治或文化區域而改變,並且需要執行定位任務的操作已知是區域設定敏感的。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
此類的物件使用以下五個變體作為其欄位:
這並不代表物件的容器;相反,它是一種辨識物件的機制。這裡 Unicode 區域設定包含用於覆蓋區域設定的預設行為的屬性和關鍵字。
文法:
public final class Locale extends Object implements Cloneable, Serializable
上面的語法表示 Locale 類別是一個最終類,這意味著沒有其他類別可以擴展該類別。
Locale 類別實作以下兩個介面的行為:
Java 語言環境類別使用 BCP 47 實作標識符,並支援用於語言環境資料交換的 LDML BCP 47 相容擴充。
Java 語言環境類別包含以下欄位:
類別有 3 個建構子:
無法指定腳本或擴充。使用上述任何一個建構函數,都可以建立一個 locale 物件並指定其字段,並且此資訊用於根據使用者的國家或地區變更要顯示的資訊。
下面給的是 java 語言環境方法:
此方法根據呼叫 JVM 實例傳回語言環境的預設物件。
範例:如果在 France JVM 上執行此函數,則會傳回法語語言的 Locale 物件。
語法 1:
public static Locale getDefault ()
語法 2:
public static Locale getDefault (Locale.Category category)
此方法用於將給定區域設定設為 JVM 實例的預設區域設定。
此方法是物件類別中 hashCode 方法的重寫方法,其中傳回該物件的 hashCode 值。
文法:
public int hashCode ()
此方法傳回 ISO 639 中提到的 2 個字母的國家/地區代碼清單。
文法:
public static String[] getISOCountries ()
此方法傳回 ISO 639 中存在的 2 個字母的語言代碼。
文法:
public static String[] getISOLanguages ()
此方法用於顯示呼叫區域物件的語言程式碼。
文法:
public String getLanguage ()
此方法用於取得給定語言環境的腳本的字串表示形式。此方法從 JDK 1.7 開始可用。
文法:
public String getDisplayScript ()
This method is used to display the name of the country to which the locale object belongs to.
Syntax 1:
public final String getDisplayCountry ()
Syntax 2:
public String getDisplayCountry (Locale locale1)
This method is used to get the ISO 3166 2-letter code for the country that the given locale belongs to.
Syntax:
public String getCountry ()
This is an inherited method from the Object class in this class that helps to check whether 2 locale objects are equal or not. It requires an object to a second locale object to be passed as an argument.
Syntax:
public Boolean equals (Object Locale2)
This method is used to display the value stored in a given locale object’s variant field according to the given user.
Syntax 1:
public final String getDisplayVariant ()
Syntax 2:
public final String getDisplayVariant (Locale locale1)
This method is used to display the given locale object’s name according to the user’s perspective.
Syntax 1:
public final String getDisplayName ()
Syntax 2:
public String getDisplayName (Locale locale1)
This method is used to copy one object’s field-to-field value to other objects of the same class. This method is an inherited method from Cloneable Interface and helps to create a clone object of a given locale object.
This method is used to get the array of all installed locales.
Syntax :
public static Locale[] getAvailableLocales
This method returns the language associated with a given locale.
Syntax 1:
public final String getDisplayLanguage ()
Syntax 2:
public final String getDisplayLanguage (Locale locale1)
This method is used to get a 3-letter abbreviation of locale country.
Syntax:
public String getISO3Country ()
This method is used to get a 3-letter abbreviation of ones locale language object.
Syntax:
public String getISO3Language ()
This method returns the String representation of the locale object.
Syntax:
public final String toString ()
This method is used to get the locale object for the given language tag string according to IETF BCP 47.
Syntax:
public static Locale forLanguageTag (String langTag)
Given below are the examples:
Example to use Locale Class calling Locale constructor.
Code:
import java.util.Locale; public class demo1 { public static void main (String[] args) { Locale object1 = new Locale ("America", "US"); Locale myObject2 = Locale.getDefault (); System.out.println ("Locale object1 name : " + object1); System.out.println ("Locale myObject2 : " + myObject2); Locale.setDefault (new Locale ("es", "ES", "WIN")); System.out.println ("Value of Default Locale after running setDefault () "+Locale.getDefault ()); System.out.println ("String Representation of NAME of locale object myobj3 " + myObject2.getDisplayName ()); System.out.println ("String Representation of language of locale object myobj3 " + myObject2.getISO3Language ()); System.out.println ("ISO3 Country Name of locale object myobj3 " + myObject2.getISO3Country ()); System.out.println ("String Representation of locale object myobj3 " + myObject2.toString ()); } }
Output:
Code:
import java.util.Locale; public class demo1 { public static void main (String[] args) { Locale myobj1= new Locale ("SPANISH", "ES", "WIN"); Locale myobj3= (Locale) myobj1.clone (); System.out.println ("nName of the country : " + myobj1.getDisplayCountry ()); System.out.println ("Name of the country of locale object myobj1 in ISO 3166 2-letter code : " + myobj1.getCountry ()); System.out.println ("nName of the country of locale object myobj3: " + myobj3.getDisplayCountry ()); System.out.println ("Name of the country with locale object myobj3 in ISO 3166 2-letter code : " + myobj3.getCountry ()); System.out.println ("Display Language of locale object myobj1 " + myobj1 .getDisplayLanguage ()); System.out.println ("ISO3 Country Name of locale object myobj3 " + myobj3.getISO3Country ()); System.out.println ("String Representation of locale object myobj3" + myobj3.toString ()); System.out.println ("List of Countries "); String[] listCountries = myobj3.getISOCountries (); for (String s:listCountries){ System.out.println (s); } } }
Output:
Code:
import java.util.Locale; public class demo1 { public static void main (String[] args) { Locale myobject1 = new Locale ("FRENCH", "FR", "WIN"); Locale myobject2 = new Locale ("FRENCH", "FR", "WIN"); System.out.println ("Variant of object 1:" + myobject1.getDisplayVariant ()); System.out.println ("Whether the two locale objects are equal:" + myobject1.equals (myobject2)); Locale[] arrLocales = Locale.getAvailableLocales (); System.out.println ("\nName of Locales are : "); for (int i = 1; i<arrLocales.length/10; i++) System.out.println (i + ":" + arrLocales[i]); } }
Output:
Locale class is a part of the java.util package that helps to alter the information according to the specific geographical or political region. A locale-sensitive operation use this class to store information related to each variant of the locale object. Many methods are provided in this class to get the information related to the locale object.
以上是本地爪哇的詳細內容。更多資訊請關注PHP中文網其他相關文章!