Java.util.Locale Class in Java | Set 1
Last Updated :
17 Feb, 2023
As the name suggests util.Locale Class is used to perform locale tasks and provides locale information for the user.
Declaration :
public final class Locale
extends Object
implements Cloneable, Serializable
Constructors :
- Locale(String L): Creates Locale from the given language code.
- Locale(String L, String C): Creates Locale from the given language, country code.
- Locale(String L, String C, String V): Creates Locale from the given language, country, variant code.
Methods:
1. getDisplayCountry() : java.util.Locale.getDisplayCountry() return the country to which locale belongs to.
Syntax :
public final String getDisplayCountry()
Parameters :
----
Return :
-----
2. getDefault() : java.util.Locale.getDefault() return the current - default value for the locale as per the JVM instance.
Syntax :
public static Locale getDefault()
Parameters :
----
Return :
current - default value for the locale as per the JVM instance.
3. getCountry() : java.util.Locale.getCountry() return the country for the locale which could be empty or ISO 3166 2-letter code.
Syntax :
public String getCountry()
Parameters :
----
Return :
-----
4. equals(Object locale2) : java.util.Locale.equals(Object locale2) checks whether two locales are equal or not.
Syntax :
public boolean equals(Object locale2)
Parameters :
locale2 : another locale to be compare with.
Return :
returns true if two locales are equal, else false.
5. clone() : java.util.Locale.clone() creates clone of the locale.
Syntax :
public Object clone()
Parameters :
----
Return :
clone of this instance
6. getAvailableLocales() : java.util.Locale.getAvailableLocales() returns array of all the installed locales.
Syntax :
public static Locale[] getAvailableLocales()
Parameters :
---
Return :
array of installed locales.
7. getDisplayLanguage() : java.util.Locale.getDisplayLanguage() returns the language with the locale.
Syntax :
public final String getDisplayLanguage()
Parameters :
----
Return :
----
8. getDisplayLanguage(Locale in) : java.util.Locale.getDisplayLanguage(Locale in) returns the language localised according to "in" Locale if possible.
Syntax :
public String getDisplayLanguage(Locale in)
Parameters :
in : the instance local
Return :
----
Exception :
NullPointerException : if "in" is null.
9. getDisplayName() : java.util.Locale.getDisplayName() displays name of the Locale
Syntax :
public final String getDisplayName()
Parameters :
----
Return :
-----------
10. getDisplayLanguage(Locale in) : java.util.Locale.getDisplayLanguage(Locale in) returns the language of "in" locale.
Syntax :
public final String getDisplayLanguage()
Parameters :
in : the instance local
Return :
----
11. getISO3Country() : java.util.Locale.getISO3Country() displays 3-letter abbreviation of Locale country.
Syntax :
public String getISO3Country()
Parameters :
----
Return :
-------------
Java
// Java Program illustrating the use of methods :
// getDisplayCountry(), getCountry(), equal(), clone(),
// getAvailableLocales(), getDefault(),
// getDisplayLanguage(Locale in), getDisplayLanguage(),
// getDisplayName(Locale in), getDisplayName(),
// getISO3Country()
import java.util.*;
public class NewClass
{
public static void main(String[] args)
{
// Creating a new Locale
Locale geek1 = new Locale("English", "IN");
// Use of getDefault() :
Locale geek2 = Locale.getDefault();
System.out.println("Locale name : " + geek1);
System.out.println("Locale name Default : " + geek2);
// Use of getDisplayCountry() :
System.out.println("\nCountry Name : "
+ geek1.getDisplayCountry());
// Use of getCountry() :
System.out.println("Country Name ISO 3166 2-letter code : "
+ geek1.getCountry());
// Use of equal() :
System.out.println("\nIs geek1 equals geek2 : "
+ geek1.equals(geek2));
// clone() : geek3 is a clone of geek2
Locale geek3 = (Locale) geek2.clone();
// Locale : geek3
System.out.println("Locale geek3 same as geek2 : "
+ geek3);
// Use of getAvailableLocales()
Locale[] geek4 = Locale.getAvailableLocales();
// We are not printing all the locales.
System.out.println("\nInstalled locales are : ");
for (int i = 1; i < geek4.length/10; i++)
System.out.println(i + ":" + geek4[i]);
// Use of getDisplayLanguage() :
System.out.println("\ngeek2 Language : "
+ geek2.getDisplayLanguage());
// Use of getDisplayLanguage(Locale in) :
System.out.println("Language of in Locale : "
+ geek1.getDisplayLanguage(new Locale("France", "French")));
// Use of getDisplayName :
System.out.println("\nUse of getDisplayName : "
+ geek1.getDisplayName());
// Use of getDisplayName(Locale in) :
System.out.println("Name of in Locale : "
+ geek2.getDisplayName(new Locale("English", "english")));
// Use of getISO3Country() :
System.out.println("\nISO3 Country Name of geek3 : "
+ geek3.getISO3Country());
}
}
Output :
Locale name : english_IN
Locale name Default : en_US
Country Name : India
Country Name ISO 3166 2-letter code : IN
Is geek1 equals geek2 : false
Locale geek3 same as geek2 : en_US
Installed locales are :
1:ar_AE
2:ar_JO
3:ar_SY
4:hr_HR
5:fr_BE
6:es_PA
7:mt_MT
8:es_VE
9:bg
10:zh_TW
11:it
12:ko
13:uk
14:lv
15:da_DK
geek2 Language : English
Language of in Locale : english
Use of getDisplayName : english (India)
Name of in Locale : English (United States)
ISO3 Country Name of geek3 : USA
Next: Java.util.Locale Class in Java | Set 2
Similar Reads
Java.util.Locale Class in Java | Set 2
Java.util.Locale Class in Java | Set 1 More methods: getDisplayVariant() : java.util.Locale.getDisplayVariant() displays variant of the Locale Syntax : public final String getDisplayVariant() Parameters : ---- Return : ----------- getDisplayVariant(Locale in) : java.util.Locale.Locale in(Locale in)
3 min read
java.time.LocalDate Class in Java
Java is the most popular programming language and widely used programming language. Java is used in all kind of application like as mobile application, desktop application, web application. In this Java java.time.LocalDate class is imported which represents to display the current date. java.time: It
4 min read
java.time.LocalTime Class in Java
Java is the most popular programming language and widely used programming language. Java is used in all kinds of applications like mobile applications, desktop applications, web applications. As in Java, java.time.LocalTime class represents time, which is viewed as hour-minute-second. This class is
5 min read
java.time.LocalDateTime Class in Java
java.time.LocalDateTime class, introduced in Java 8, represents a local date-time object without timezone information. The LocalDateTime class in Java is an immutable date-time object that represents a date in the yyyy-MM-dd-HH-mm-ss.zzz format. It implements the ChronoLocalDateTime interface and in
4 min read
Java.net.URLEncoder class in Java
This class is a utility class for HTML form encoding. Encoding makes the form of URL more reliable and secure. When the user request is triggered by a get method, the form parameters and their values are appended at the end of URL after a '?' sign. The problem arises when special characters are used
3 min read
Java.util Package in Java
Java.util PackageIt contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes (a string tokenizer, a random-number generator, and a bit array).Following are the Important Classes in Java.util package
4 min read
Java.lang.ThreadLocal Class in Java
This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable. Basically, it is another way to achieve thread safety apart from writing im
4 min read
Java.io.Printstream Class in Java | Set 2
Java.io.Printstream Class in Java | Set 1More Methods: PrintStream printf(Locale l, String format, Object... args) : A convenience method to write a formatted string to this output stream using the specified format string and arguments. Syntax :public PrintStream printf(Locale l, String format, Obje
6 min read
Java.io.PrintWriter class in Java | Set 2
Java.io.PrintWriter class in Java | Set 1 More methods: PrintWriter printf(Locale l, String format, Object... args) : A convenience method to write a formatted string to this writer using the specified format string and arguments. Syntax :public PrintWriter printf(Locale l, String format, Object...
7 min read
Java.lang.Character.UnicodeBlock Class in Java
Character.UnicodeBlock Class represents particular Character blocks of the Unicode(standards using hexadecimal values to express characters - 16 bit) specifications. Character Blocks define characters used for specific purpose. Declaration : public static final class Character.UnicodeBlock extends C
2 min read