Java.util - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.util Locale

Java.util Locale

shape Description

A locale is a course of action of parameters which addresses specific district, for instance, geographic region or political territory that has comparable lingo and conventions. A region challenge may consolidate customer's tongue, country, and any remarkable variety slants that customer needs. A Locale class which obliges operation to play out its endeavor is called as region touchy and uses Locale to offer information to the customer. Following is the syntax declaration for the java.util Locale class. [java] public final class Locale extends Object implements Cloneable, Serializable [/java]

Constructors Of java.util Locale Class

shape Constructors

Following are the constructors of java.util Locale Class.
Constructors Description
Locale(String, String) To build locale from language, country.
Locale(String, String, String) To build locale from language, country, variant..

Methods Of java.util Locale Class

shape Methods

Following are the methods of java.util Locale Class.
Methods Description
clone() To override Cloneable
equals(Object) To compare two objects for the purpose of equality.
getCountry() To get the country code.
getDefault() To get the default locale.
getDisplayName() To get the name of locale.
getLanguage() To get the language code.
getVariant() To get variant code.

By Using java.util.Locale.clone() Method

shape Description

Following is the syntax declaration of the method. [java]public Object clone()[/java] DemoLocale.java [java] package com.SPlessons; import java.util.*; public class DemoLocale { public static void main(String[] args) { // create a new locale Locale locale1 = new Locale("ENGLISH", "US"); // print this locale System.out.println("Locale1:" + locale1); // create a second locale Locale locale2; // clone locale1 to locale2 locale2 = (Locale) locale1.clone(); // print locale2 System.out.println("Locale2:" + locale2); } } [/java] The clone() method has been performed here. Output: Now compile the code result will be as follows. [java] Locale1:english_US Locale2:english_US [/java]

By Using java.util.Locale.equals() Method

shape Description

Following is the syntax declaration of the method. [java]public boolean equals(Object obj)[/java] DemoLocale.java [java] package com.SPlessons; import java.util.*; public class DemoLocale { public static void main(String[] args) { // create a new locale Locale locale1 = new Locale("ENGLISH", "US"); // print this locale System.out.println("Locale1:" + locale1); // create a second locale Locale locale2 = new Locale("GERMANY", "GERMAN"); // compare two locales System.out.println("Locales are equal:" + locale1.equals(locale2)); // create a third locale Locale locale3 = new Locale("ENGLISH", "US"); // compare locale1 and locale3 System.out.println("Locales are equal:" + locale1.equals(locale3)); } } [/java] In the above example, there locale 1 and locale 2 have the same elements. Output: Now compile the code result will be as follows. [java] Locale1:english_US Locales are equal:false Locales are equal:true [/java]

By Using java.util.Locale.getDefault() Method

shape Description

Following is the syntax declaration of the method. [java]public static Locale getDefault()[/java] DemoLocale.java [java] package com.SPlessons; import java.util.*; public class DemoLocale { public static void main(String[] args) { // create a new locale Locale locale = Locale.getDefault(); // print the default locale System.out.println("Locale:" + locale); } } [/java] The default locale of the system is going to be generated in the result. Output: Now compile the code result will be as follows. [java] Locale:en_US [/java]

By Using java.util.Locale.getLanguage() Method

shape Description

Following is the syntax declaration of the method. [java]public String getLanguage()[/java] DemoLocale.java [java] package com.SPlessons; import java.util.*; public class DemoLocale { public static void main(String[] args) { // create a new locale Locale locale = new Locale("en", "US", "WIN"); // print locale System.out.println("Locale:" + locale); // get language and print it System.out.println("Language:" + locale.getLanguage()); } } [/java] Output: Now compile the code result will be as follows. [java] Locale:en1_US_WIN Language:en1 [/java]

By Using java.util.Locale.getVariant() Method

shape Description

Following is the syntax declaration of the method. [java]public String getVariant()[/java] DemoLocale.java [java] package com.SPlessons; import java.util.*; public class DemoLocale { public static void main(String[] args) { // create a new locale Locale locale = new Locale("WIN", "US", "en"); // print locale System.out.println("Locale:" + locale); // get variant and print it System.out.println("Language:" + locale.getVariant()); } } [/java] Output: Now compile the code result will be as follows. [java] Locale:win_US_en Language:en [/java]

Summary

shape Key Points

  • The int hashCode() method is used to override hash code.
  • The getISO3Language() method is used to return 3 letter abbreviation for the locale.
  • The getDisplayLanguage() is utilized to give back the name for the locale.