Java Locale Class

Introduction

The Locale class in Java, part of the java.util package, represents a specific geographical, political, or cultural region. It is used to tailor the behavior of locale-sensitive operations such as formatting dates, numbers, and currencies.

Table of Contents

  1. What is the Locale Class?
  2. Common Methods
  3. Examples of Using the Locale Class
  4. Conclusion

1. What is the Locale Class?

The Locale class provides information about a specific locale, including the language, country, and variant. It is essential for internationalization (i18n) and localization (l10n) in Java applications.

2. Common Methods

  • Locale(String language): Constructs a locale with the specified language.
  • Locale(String language, String country): Constructs a locale with the specified language and country.
  • Locale(String language, String country, String variant): Constructs a locale with the specified language, country, and variant.
  • getLanguage(): Returns the language code of this locale.
  • getCountry(): Returns the country code of this locale.
  • getDisplayName(): Returns a name for the locale that is appropriate for display to the user.
  • getDisplayLanguage(): Returns the name of the locale’s language suitable for display to the user.
  • getDisplayCountry(): Returns the name of the locale’s country suitable for display to the user.
  • getDefault(): Returns the default locale for this instance of the Java Virtual Machine.
  • setDefault(Locale newLocale): Sets the default locale for this instance of the Java Virtual Machine.

3. Examples of Using the Locale Class

Example 1: Creating a Locale

This example demonstrates how to create a Locale object with a specific language.

import java.util.Locale;

public class CreateLocaleExample {
    public static void main(String[] args) {
        Locale locale = new Locale("en");
        System.out.println("Language: " + locale.getLanguage());
    }
}

Output:

Language: en

Example 2: Creating a Locale with Language and Country

This example shows how to create a Locale object with both language and country.

import java.util.Locale;

public class CreateLocaleWithCountryExample {
    public static void main(String[] args) {
        Locale locale = new Locale("en", "US");
        System.out.println("Language: " + locale.getLanguage());
        System.out.println("Country: " + locale.getCountry());
    }
}

Output:

Language: en
Country: US

Example 3: Displaying Locale Information

This example demonstrates how to display various locale information.

import java.util.Locale;

public class DisplayLocaleInfoExample {
    public static void main(String[] args) {
        Locale locale = new Locale("fr", "FR");
        System.out.println("Locale: " + locale.getDisplayName());
        System.out.println("Language: " + locale.getDisplayLanguage());
        System.out.println("Country: " + locale.getDisplayCountry());
    }
}

Output:

Locale: French (France)
Language: French
Country: France

Example 4: Getting the Default Locale

This example shows how to get the default locale of the Java Virtual Machine.

import java.util.Locale;

public class DefaultLocaleExample {
    public static void main(String[] args) {
        Locale defaultLocale = Locale.getDefault();
        System.out.println("Default Locale: " + defaultLocale.getDisplayName());
    }
}

Output:

Default Locale: English (India)

Example 5: Setting the Default Locale

This example demonstrates how to set the default locale of the Java Virtual Machine.

import java.util.Locale;

public class SetDefaultLocaleExample {
    public static void main(String[] args) {
        Locale newLocale = new Locale("es", "ES");
        Locale.setDefault(newLocale);
        Locale defaultLocale = Locale.getDefault();
        System.out.println("New Default Locale: " + defaultLocale.getDisplayName());
    }
}

Output:

New Default Locale: espa?ol (Espa?a)

Example 6: Using Locale with Date Formatting

This example shows how to use a Locale with date formatting.

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

public class LocaleDateFormatExample {
    public static void main(String[] args) {
        Date date = new Date();
        Locale locale = new Locale("de", "DE");
        DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG, locale);
        System.out.println("Formatted Date: " + dateFormat.format(date));
    }
}

Output:

Formatted Date: 1. Juli 2024

Example 7: Using Locale with Number Formatting

This example demonstrates how to use a Locale with number formatting.

import java.text.NumberFormat;
import java.util.Locale;

public class LocaleNumberFormatExample {
    public static void main(String[] args) {
        double number = 12345.678;
        Locale locale = new Locale("fr", "FR");
        NumberFormat numberFormat = NumberFormat.getInstance(locale);
        System.out.println("Formatted Number: " + numberFormat.format(number));
    }
}

Output:

Formatted Number: 12?345,678

Example 8: Using Locale with Currency Formatting

This example shows how to use a Locale with currency formatting.

import java.text.NumberFormat;
import java.util.Locale;

public class LocaleCurrencyFormatExample {
    public static void main(String[] args) {
        double amount = 12345.678;
        Locale locale = new Locale("ja", "JP");
        NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(locale);
        System.out.println("Formatted Currency: " + currencyFormat.format(amount));
    }
}

Output:

Formatted Currency: ?12,346

Example 9: Listing All Available Locales

This example demonstrates how to list all available locales.

import java.util.Locale;

public class ListAvailableLocalesExample {
    public static void main(String[] args) {
        Locale[] locales = Locale.getAvailableLocales();
        for (Locale locale : locales) {
            System.out.println(locale.getDisplayName() + " (" + locale.getLanguage() + "_" + locale.getCountry() + ")");
        }
    }
}

Output:

Output is list of all supported languages.

Example 10: Using Builder to Create Locale

This example shows how to use Locale.Builder to create a Locale.

import java.util.Locale;

public class LocaleBuilderExample {
    public static void main(String[] args) {
        Locale locale = new Locale.Builder()
                          .setLanguage("en")
                          .setRegion("GB")
                          .build();
        System.out.println("Locale: " + locale.getDisplayName());
    }
}

Output:

Locale: English (United Kingdom)

4. Conclusion

The Locale class in Java provides essential support for internationalization (i18n) and localization (l10n). It allows developers to tailor applications to different regions and cultures, ensuring that content is appropriately formatted for various locales. The examples provided demonstrate common usage patterns and highlight the capabilities of the Locale class.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top