java.util.Locale

The Locale class represents a specific geographical, political, or cultural region. It is used to tailor data (such as date formats, number formats, or messages) to the rules and conventions of a specific region.

Each Locale object consists of:

  • A language code (like "en" for English),
  • An optional country code (like "US" for United States),
  • And an optional variant (used for special cases).

Commonly Used Methods

Simple Program: Display Locale Details

import java.util.Locale;

public class LocaleDetails {
    public static void main(String[] args) {
        Locale locale = new Locale("fr", "FR");

        System.out.println("Language Code: " + locale.getLanguage());
        System.out.println("Country Code: " + locale.getCountry());
        System.out.println("Display Language: " + locale.getDisplayLanguage());
        System.out.println("Display Country: " + locale.getDisplayCountry());
    }
}

Problem Statement:

LotusJavaPrince is building a multilingual banking app. He wants to list the display names of supported locales to show language selection options to the user.

import java.util.Locale;

public class LocaleCaseStudy {
    public static void main(String[] args) {
        Locale[] supportedLocales = {
            new Locale("en", "US"),
            new Locale("hi", "IN"),
            new Locale("fr", "FR"),
            new Locale("ja", "JP"),
            new Locale("ar", "SA")
        };

        System.out.println("Supported Locales in LotusJavaPrince's App:");
        for (Locale locale : supportedLocales) {
            System.out.println("-----------------------------------------");
            System.out.println("Locale Code: " + locale.toString());
            System.out.println("Display Language: " + locale.getDisplayLanguage(locale));
            System.out.println("Display Country: " + locale.getDisplayCountry(locale));
        }
    }
}

Output

🌍 Supported Locales in LotusJavaPrince’s App:
-----------------------------------------
Locale Code: en_US
Display Language: English
Display Country: United States
-----------------------------------------
Locale Code: hi_IN
Display Language: हिंदी
Display Country: भारत
-----------------------------------------
Locale Code: fr_FR
Display Language: français
Display Country: France
-----------------------------------------
Locale Code: ja_JP
Display Language: 日本語
Display Country: 日本
-----------------------------------------
Locale Code: ar_SA
Display Language: العربية
Display Country: المملكة العربية السعودية

java.util.Locale is central to building internationalized applications.It represents a region and is used by classes like DateFormat, NumberFormat, Currency, and ResourceBundle.By pairing Locale with other classes, you can adapt your application to global users easily.

Scroll to Top