It would be much easier if there were the same electrical outlets all over the world, the principle of writing numbers and dates. However, this is not yet the case, and programmers have to take this into account. In particular, java.util.Locale class allows you to take into account the peculiarities of regional representations of the alphabet, symbols, numbers, and dates. In this article, we will look at the Java Locale class and give examples of its use.
What is Locale
When you install your Microsoft Windows operating system, you will be asked about your country and languages. In fact, such moments are associated with the use of Locale. It can be said that you create an object of the Locale class, specifying the country and language. This pulls up other related settings, such as displaying the date and time. The Java Virtual Machine pulls the regional settings from the operating system. However, the default locale in Java can be changed using a special method if it’s necessary.Creating objects of Java Locale Class
For some countries, locale settings are set using constants, for example:
Locale.US
Locale.UK
For all other countries, an object of the Locale class must be created using a constructor, for example:
Locale ukr = new Locale("ukr", "UA");
There is a Locale.ROOT constant. Locale root represents the locale where language and country are equal to the empty string(""). This locale is the base locale for all other locales. Used for writing locale-independent applications. Let’s take a small example.
import java.util.Locale;
public class LocaleExample1 {
public static void main(String[] args) {
var uk = Locale.UK;
System.out.println(uk);
Locale ua = new Locale("ukr", "UA");
System.out.println(ua);
}
}
Here is the output:
en_GB
ukr_UA
Locale class Methods. How to determine the current Locale or change it
There are many methods of Locale class in Java. You can learn them from Javadoc. Here we take a glimpse only on a few of them. You can determine the current locale setting using the method Locale.getDefault():
Locale current = Locale.getDefault();
You can change the current locale for the current instance of the JVM using the method setDefault():
Locale.setDefault(Locale.CANADA);
Method String getCountry() returns the country or region code for this locale. It could either be the empty string (an uppercase ISO 3166 2-letter code) or a UN M.49 3-digit code.
String getDisplayCountry() returns a name for the locale's country that is appropriate for display to the user.
String getDisplayCountry(Locale inLocale) returns a name for the locale's country that is appropriate for display to the user.
String getDisplayLanguage() returns a name for the locale's language that is appropriate for display to the user.
String getDisplayLanguage(Locale inLocale) returns a name for the locale's language that is appropriate for display to the user.
String getLanguage() returns the language code of the Locale.
Locale Class Example
import java.util.Locale;
//Locale Example
public class LocaleExample1 {
public static void main(String[] args) {
var uk = Locale.UK;
System.out.println(uk);
Locale ua = new Locale("ukr", "UA");
System.out.println(ua);
Locale.setDefault(Locale.CANADA);
Locale current = Locale.getDefault();
getLocaleInfo(current);
getLocaleInfo(ua);
getLocaleInfo(uk);
}
private static void getLocaleInfo(Locale current) {
System.out.println("Country code: " + current.getCountry());
System.out.println("Name of the Country: " + current.getDisplayCountry());
System.out.println("Language Code: " + current.getLanguage());
System.out.println("Language Name: "
+ current.getDisplayLanguage());
System.out.println();
}
}
Here is the output:
en_GB
ukr_UA
Region code: CA
Name of the Country: Canada
Language Code: en
Language Name: English
Region code: UA
Name of the Country: Ukraine
Language Code: ukr
Language Name: Ukrainian
GO TO FULL VERSION