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中文网其他相关文章!