Java : Locale with Examples

Locale (Java SE 18 & JDK 18) API Examples.
You will find code examples on most Locale methods.


Summary

A Locale object represents a specific geographical, political, or cultural region.

Class diagram

final var us = Locale.getDefault();

System.out.println(us.toLanguageTag()); // en-US
System.out.println(us.getLanguage()); // en
System.out.println(us.getCountry()); // US

final var japan = Locale.JAPAN;

System.out.println(japan.toLanguageTag()); // ja-JP
System.out.println(japan.getLanguage()); // ja
System.out.println(japan.getCountry()); // JP

final var week = DayOfWeek.FRIDAY;
System.out.println(week.getDisplayName(TextStyle.FULL, us)); // Friday
System.out.println(week.getDisplayName(TextStyle.FULL, japan)); // 金曜日

final var value = 1234;
System.out.println(NumberFormat.getCurrencyInstance(us).format(value)); // $1,234.00
System.out.println(NumberFormat.getCurrencyInstance(japan).format(value)); // ¥1,234

Fields

static final Locale CANADA

Useful constant for country.

final var locale = Locale.CANADA;
System.out.println(locale.toLanguageTag()); // en-CA

static final Locale CANADA_FRENCH

Useful constant for country.

final var locale = Locale.CANADA_FRENCH;
System.out.println(locale.toLanguageTag()); // fr-CA

static final Locale CHINA

Useful constant for country.

final var locale = Locale.CHINA;
System.out.println(locale.toLanguageTag()); // zh-CN

static final Locale CHINESE

Useful constant for language.

final var locale = Locale.CHINESE;
System.out.println(locale.toLanguageTag()); // zh

static final Locale ENGLISH

Useful constant for language.

final var locale = Locale.ENGLISH;
System.out.println(locale.toLanguageTag()); // en

static final Locale FRANCE

Useful constant for country.

final var locale = Locale.FRANCE;
System.out.println(locale.toLanguageTag()); // fr-FR

static final Locale FRENCH

Useful constant for language.

final var locale = Locale.FRENCH;
System.out.println(locale.toLanguageTag()); // fr

static final Locale GERMAN

Useful constant for language.

final var locale = Locale.GERMAN;
System.out.println(locale.toLanguageTag()); // de

static final Locale GERMANY

Useful constant for country.

final var locale = Locale.GERMANY;
System.out.println(locale.toLanguageTag()); // de-DE

static final Locale ITALIAN

Useful constant for language.

final var locale = Locale.ITALIAN;
System.out.println(locale.toLanguageTag()); // it

static final Locale ITALY

Useful constant for country.

final var locale = Locale.ITALY;
System.out.println(locale.toLanguageTag()); // it-IT

static final Locale JAPAN

Useful constant for country.

final var locale = Locale.JAPAN;
System.out.println(locale.toLanguageTag()); // ja-JP

static final Locale JAPANESE

Useful constant for language.

final var locale = Locale.JAPANESE;
System.out.println(locale.toLanguageTag()); // ja

static final Locale KOREA

Useful constant for country.

final var locale = Locale.KOREA;
System.out.println(locale.toLanguageTag()); // ko-KR

static final Locale KOREAN

Useful constant for language.

final var locale = Locale.KOREAN;
System.out.println(locale.toLanguageTag()); // ko

static final Locale PRC

Useful constant for country.

final var locale = Locale.PRC;
System.out.println(locale.toLanguageTag()); // zh-CN

static final char PRIVATE_USE_EXTENSION

The key for the private use extension ('x').

final var ext = Locale.PRIVATE_USE_EXTENSION;
System.out.println(ext); // x

static final Locale ROOT

Useful constant for the root locale.

final var locale = Locale.ROOT;
System.out.println(locale.toLanguageTag()); // und

static final Locale SIMPLIFIED_CHINESE

Useful constant for language.

final var locale = Locale.SIMPLIFIED_CHINESE;
System.out.println(locale.toLanguageTag()); // zh-CN

static final Locale TAIWAN

Useful constant for country.

final var locale = Locale.TAIWAN;
System.out.println(locale.toLanguageTag()); // zh-TW

static final Locale TRADITIONAL_CHINESE

Useful constant for language.

final var locale = Locale.TRADITIONAL_CHINESE;
System.out.println(locale.toLanguageTag()); // zh-TW

static final Locale UK

Useful constant for country.

final var locale = Locale.UK;
System.out.println(locale.toLanguageTag()); // en-GB

static final char UNICODE_LOCALE_EXTENSION

The key for Unicode locale extension ('u').

final var ext = Locale.UNICODE_LOCALE_EXTENSION;
System.out.println(ext); // u

static final Locale US

Useful constant for country.

final var locale = Locale.US;
System.out.println(locale.toLanguageTag()); // en-US

Constructors

Locale (String language)

Construct a locale from a language code.

This method is legacy. It is recommended to use the forLanguageTag method or Locale.Builder instead.

Locale (String language, String country)

Construct a locale from language and country.

This method is legacy. It is recommended to use the forLanguageTag method or Locale.Builder instead.

Locale (String language, String country, String variant)

Construct a locale from language, country and variant.

This method is legacy. It is recommended to use the forLanguageTag method or Locale.Builder instead.

Methods

Object clone ()

Overrides Cloneable.

final var locale = Locale.US;
System.out.println(locale.toLanguageTag()); // en-US

final var cloned = locale.clone();
System.out.println(locale.toLanguageTag()); // en-US

System.out.println(locale.equals(cloned)); // true

boolean equals (Object obj)

Returns true if this Locale is equal to another object.

Please see clone().

static List<Locale> filter (List<Locale.LanguageRange> priorityList, Collection<Locale> locales)

Returns a list of matching Locale instances using the filtering mechanism defined in RFC 4647.

final var priorityList = List.of(
        new Locale.LanguageRange("en-US"),
        new Locale.LanguageRange("zh-Hant")
);

final var locales = List.of(
        Locale.forLanguageTag("zh"),
        Locale.forLanguageTag("zh-Hant"),
        Locale.forLanguageTag("zh-Hant-TW"),
        Locale.forLanguageTag("en"),
        Locale.forLanguageTag("en-US")
);

System.out.println("-- filter --");
final var ret = Locale.filter(priorityList, locales);
ret.forEach(locale -> {
    System.out.println(locale.toLanguageTag());
});

// Result
// ↓
//-- filter --
//en-US
//zh-Hant
//zh-Hant-TW

static List<Locale> filter (List<Locale.LanguageRange> priorityList, Collection<Locale> locales, Locale.FilteringMode mode)

Returns a list of matching Locale instances using the filtering mechanism defined in RFC 4647.

Please see also : filter(List<Locale.LanguageRange> priorityList, Collection<Locale> locales)

final var priorityList = List.of(
        new Locale.LanguageRange("de-DE")
);

final var locales = List.of(
        Locale.forLanguageTag("de"),
        Locale.forLanguageTag("de-DE"),
        Locale.forLanguageTag("de-Deva"),
        Locale.forLanguageTag("de-Deva-DE"),
        Locale.forLanguageTag("de-DE-1996"),
        Locale.forLanguageTag("de-Latn-DE"),
        Locale.forLanguageTag("de-Latn-DE-1996")
);

System.out.println("-- filter --");
final var ret1 = Locale.filter(
        priorityList, locales, Locale.FilteringMode.AUTOSELECT_FILTERING);
ret1.forEach(locale -> {
    System.out.println(locale.toLanguageTag());
});

// Result
// ↓
//-- filter --
//de-DE
//de-DE-1996

System.out.println("-- filter --");
final var ret2 = Locale.filter(
        priorityList, locales, Locale.FilteringMode.EXTENDED_FILTERING);
ret2.forEach(locale -> {
    System.out.println(locale.toLanguageTag());
});

// Result
// ↓
//-- filter --
//de-DE
//de-Deva-DE
//de-DE-1996
//de-Latn-DE
//de-Latn-DE-1996

static List<String> filterTags (List<Locale.LanguageRange> priorityList, Collection<String> tags)

Returns a list of matching languages tags using the basic filtering mechanism defined in RFC 4647.

final var priorityList = List.of(
        new Locale.LanguageRange("en-US"),
        new Locale.LanguageRange("zh-Hant")
);

final var tags = List.of(
        "zh",
        "zh-Hant",
        "zh-Hant-TW",
        "en",
        "en-US"
);

System.out.println("-- filterTags --");
final var ret = Locale.filterTags(priorityList, tags);
ret.forEach(tag -> {
    System.out.println(tag);
});

// Result
// ↓
//--- filterTags --
//en-US
//zh-Hant
//zh-Hant-TW

static List<String> filterTags (List<Locale.LanguageRange> priorityList, Collection<String> tags, Locale.FilteringMode mode)

Returns a list of matching languages tags using the basic filtering mechanism defined in RFC 4647.

Please see also : filterTags(List<Locale.LanguageRange> priorityList, Collection<String> tags)

final var priorityList = List.of(
        new Locale.LanguageRange("de-DE")
);

final var tags = List.of(
        "de",
        "de-DE",
        "de-Deva",
        "de-Deva-DE",
        "de-DE-1996",
        "de-Latn-DE",
        "de-Latn-DE-1996"
);

System.out.println("-- filterTags --");
final var ret1 = Locale.filterTags(
        priorityList, tags, Locale.FilteringMode.AUTOSELECT_FILTERING);
ret1.forEach(tag -> {
    System.out.println(tag);
});

// Result
// ↓
//-- filterTags --
//de-DE
//de-DE-1996

System.out.println("-- filterTags --");
final var ret2 = Locale.filterTags(
        priorityList, tags, Locale.FilteringMode.EXTENDED_FILTERING);
ret2.forEach(tag -> {
    System.out.println(tag);
});

// Result
// ↓
//-- filterTags --
//de-DE
//de-Deva-DE
//de-DE-1996
//de-Latn-DE
//de-Latn-DE-1996

static Locale forLanguageTag (String languageTag)

Returns a locale for the specified IETF BCP 47 language tag string.

final var us = Locale.forLanguageTag("en-US");

System.out.println(us.toLanguageTag()); // en-US
System.out.println(us.getLanguage()); // en
System.out.println(us.getCountry()); // US

final var japan = Locale.forLanguageTag("ja-JP");

System.out.println(japan.toLanguageTag()); // ja-JP
System.out.println(japan.getLanguage()); // ja
System.out.println(japan.getCountry()); // JP
final var locale = Locale.forLanguageTag("th-TH-u-nu-thai-x-lvariant-TH");

System.out.println(locale.toLanguageTag()); // th-TH-u-nu-thai-x-lvariant-TH
System.out.println(locale.getLanguage()); // th
System.out.println(locale.getCountry()); // TH
System.out.println(locale.getVariant()); // TH

System.out.println("-- extensions --");
for (final var key : locale.getExtensionKeys()) {
    System.out.printf("key = %c : extension = %s%n", key, locale.getExtension(key));
}

// Result
// ↓
//-- extensions --
//key = u : extension = nu-thai

static Locale[] getAvailableLocales ()

Returns an array of all installed locales.

final var locales = Locale.getAvailableLocales();

for (final var locale : locales) {
    System.out.println(locale.toLanguageTag());
}

// Result
// ↓
//und
//he
//th-Thai-TH
//nds
//tk-Latn-TM
//ti-ET
//ta-SG
//lv
//en-NU
//zh-Hans-SG
//ff-Adlm-LR
//en-JM
//...

String getCountry ()

Returns the country/region code for this locale, which should either be the empty string, an uppercase ISO 3166 2-letter code, or a UN M.49 3-digit code.

final var us = Locale.US;

System.out.println(us.toLanguageTag()); // en-US
System.out.println(us.getLanguage()); // en
System.out.println(us.getCountry()); // US

final var english = Locale.ENGLISH;

System.out.println(english.toLanguageTag()); // en
System.out.println(english.getLanguage()); // en
System.out.println(english.getCountry().isEmpty()); // true
final var japan = Locale.JAPAN;

System.out.println(japan.toLanguageTag()); // ja-JP
System.out.println(japan.getLanguage()); // ja
System.out.println(japan.getCountry()); // JP

final var japanese = Locale.JAPANESE;

System.out.println(japanese.toLanguageTag()); // ja
System.out.println(japanese.getLanguage()); // ja
System.out.println(japanese.getCountry().isEmpty()); // true

static Locale getDefault ()

Gets the current value of the default locale for this instance of the Java Virtual Machine.

final var locale = Locale.getDefault();
System.out.println(locale.toLanguageTag()); // en-US

{
    final var date = LocalDate.of(2022, 12, 31);
    final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
    System.out.println(date.format(formatter)); // Saturday, December 31, 2022
}

Locale.setDefault(Locale.JAPAN);

final var newLocale = Locale.getDefault();
System.out.println(newLocale.toLanguageTag()); // ja-JP

{
    final var date = LocalDate.of(2022, 12, 31);
    final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
    System.out.println(date.format(formatter)); // 2022年12月31日土曜日
}

static Locale getDefault (Locale.Category category)

Gets the current value of the default locale for the specified Category for this instance of the Java Virtual Machine.

final var locale = Locale.getDefault(Locale.Category.FORMAT);
System.out.println(locale.toLanguageTag()); // en-US

{
    final var currency = NumberFormat.getCurrencyInstance();
    System.out.println(currency.format(1234)); // $1,234.00
}

Locale.setDefault(Locale.Category.FORMAT, Locale.JAPAN);

final var newLocale = Locale.getDefault(Locale.Category.FORMAT);
System.out.println(newLocale.toLanguageTag()); // ja-JP

{
    final var currency = NumberFormat.getCurrencyInstance();
    System.out.println(currency.format(1234)); // ¥1,234
}

final String getDisplayCountry ()

Returns a name for the locale's country that is appropriate for display to the user.

System.out.println(Locale.getDefault().toLanguageTag()); // en-US

final var locale = Locale.JAPAN;
System.out.println(locale.toLanguageTag()); // ja-JP

System.out.println(locale.getDisplayCountry()); // Japan
System.out.println(locale.getDisplayCountry(Locale.JAPANESE)); // 日本

String getDisplayCountry (Locale inLocale)

Returns a name for the locale's country that is appropriate for display to the user.

Please see getDisplayCountry().

final String getDisplayLanguage ()

Returns a name for the locale's language that is appropriate for display to the user.

System.out.println(Locale.getDefault().toLanguageTag()); // en-US

final var locale = Locale.JAPAN;
System.out.println(locale.toLanguageTag()); // ja-JP

System.out.println(locale.getDisplayLanguage()); // Japanese
System.out.println(locale.getDisplayLanguage(Locale.JAPANESE)); // 日本語

String getDisplayLanguage (Locale inLocale)

Returns a name for the locale's language that is appropriate for display to the user.

Please see getDisplayLanguage().

final String getDisplayName ()

Returns a name for the locale that is appropriate for display to the user.

System.out.println(Locale.getDefault().toLanguageTag()); // en-US

final var locale = Locale.JAPAN;
System.out.println(locale.toLanguageTag()); // ja-JP

System.out.println(locale.getDisplayName()); // Japanese (Japan)
System.out.println(locale.getDisplayName(Locale.JAPANESE)); // 日本語 (日本)

String getDisplayName (Locale inLocale)

Returns a name for the locale that is appropriate for display to the user.

Please see getDisplayName().

String getDisplayScript ()

Returns a name for the locale's script that is appropriate for display to the user.

System.out.println(Locale.getDefault().toLanguageTag()); // en-US

final var locale = Locale.forLanguageTag("en-Latn-US");
System.out.println(locale.toLanguageTag()); // en-Latn-US

System.out.println(locale.getDisplayScript()); // Latin
System.out.println(locale.getDisplayScript(Locale.JAPANESE)); // ラテン文字

String getDisplayScript (Locale inLocale)

Returns a name for the locale's script that is appropriate for display to the user.

Please see getDisplayScript().

final String getDisplayVariant ()

Returns a name for the locale's variant code that is appropriate for display to the user.

System.out.println(Locale.getDefault().toLanguageTag()); // en-US

final var locale = Locale.forLanguageTag("en-US-POSIX");
System.out.println(locale.toLanguageTag()); // en-US-POSIX

System.out.println(locale.getDisplayVariant()); // Computer
System.out.println(locale.getDisplayVariant(Locale.JAPANESE)); // コンピュータ

String getDisplayVariant (Locale inLocale)

Returns a name for the locale's variant code that is appropriate for display to the user.

Please see getDisplayVariant().

String getExtension (char key)

Returns the extension (or private use) value associated with the specified key, or null if there is no extension associated with the key.

final var locale = Locale.forLanguageTag("ja-JP-u-ca-japanese-x-lvariant-JP");
System.out.println(locale.toLanguageTag()); // ja-JP-u-ca-japanese-x-lvariant-JP
System.out.println(locale.hasExtensions()); // true

System.out.println("-- extensions --");
for (final var key : locale.getExtensionKeys()) {
    System.out.printf("key = %c : extension = %s%n", key, locale.getExtension(key));
}

// Result
// ↓
//-- extensions --
//key = u : extension = ca-japanese

Set<Character> getExtensionKeys ()

Returns the set of extension keys associated with this locale, or the empty set if it has no extensions.

Please see getExtension(char key).

String getISO3Country ()

Returns a three-letter abbreviation for this locale's country.

final var us = Locale.US;
System.out.println(us.toLanguageTag()); // en-US
System.out.println(us.getISO3Country()); // USA
System.out.println(us.getISO3Language()); // eng

final var japan = Locale.JAPAN;
System.out.println(japan.toLanguageTag()); // ja-JP
System.out.println(japan.getISO3Country()); // JPN
System.out.println(japan.getISO3Language()); // jpn

String getISO3Language ()

Returns a three-letter abbreviation of this locale's language.

Please see getISO3Country().

static String[] getISOCountries ()

Returns a list of all 2-letter country codes defined in ISO 3166.

final var countries = Locale.getISOCountries();

for (final var country : countries) {
    System.out.println(country);
}

// Result
// ↓
//AD
//AE
//AF
//AG
//AI
//AL
//AM
//AO
//AQ
//AR
//AS
//...

static Set<String> getISOCountries (Locale.IsoCountryCode type)

Returns a Set of ISO3166 country codes for the specified type.

final var countries = Locale.getISOCountries(Locale.IsoCountryCode.PART1_ALPHA3);

for (final var country : countries.stream().sorted().toList()) {
    System.out.println(country);
}

// Result
// ↓
//ABW
//AFG
//AGO
//AIA
//ALA
//ALB
//...

static String[] getISOLanguages ()

Returns a list of all 2-letter language codes defined in ISO 639.

final var languages = Locale.getISOLanguages();

for (final var language : languages) {
    System.out.println(language);
}

// Result
// ↓
//aa
//ab
//ae
//af
//ak
//am
//an
//ar
//as
//av
//ay
//az
//ba
//...

String getLanguage ()

Returns the language code of this Locale.

Please see getCountry().

String getScript ()

Returns the script for this locale, which should either be the empty string or an ISO 15924 4-letter script code.

final var locale = Locale.forLanguageTag("en-Latn-US");
System.out.println(locale.toLanguageTag()); // en-Latn-US

System.out.println(locale.getScript()); // Latn

Set<String> getUnicodeLocaleAttributes ()

Returns the set of unicode locale attributes associated with this locale, or the empty set if it has no attributes.

final var locale = new Locale.Builder()
        .setLanguage("test")
        .addUnicodeLocaleAttribute("abc")
        .addUnicodeLocaleAttribute("xyz")
        .build();

System.out.println(locale.toLanguageTag()); // test-u-abc-xyz

final var attributes = locale.getUnicodeLocaleAttributes();
System.out.println(attributes); // [abc, xyz]

Set<String> getUnicodeLocaleKeys ()

Returns the set of Unicode locale keys defined by this locale, or the empty set if this locale has none.

final var locale = Locale.forLanguageTag("ja-JP-u-ca-japanese-x-lvariant-JP");
System.out.println(locale.toLanguageTag()); // ja-JP-u-ca-japanese-x-lvariant-JP

final var keys = locale.getUnicodeLocaleKeys();
System.out.println(keys); // [ca]

final var type = locale.getUnicodeLocaleType("ca");
System.out.println(type); // japanese

String getUnicodeLocaleType (String key)

Returns the Unicode locale type associated with the specified Unicode locale key for this locale.

Please see getUnicodeLocaleKeys().

String getVariant ()

Returns the variant code for this locale.

final var locale = Locale.forLanguageTag("en-US-POSIX");
System.out.println(locale.toLanguageTag()); // en-US-POSIX

System.out.println(locale.getVariant()); // POSIX

boolean hasExtensions ()

Returns true if this Locale has any extensions.

Please see getExtension(char key).

int hashCode ()

Override hashCode.

System.out.println(Locale.US.hashCode()); // 96636889
System.out.println(Locale.JAPAN.hashCode()); // 100856547

static Locale lookup (List<Locale.LanguageRange> priorityList, Collection<Locale> locales)

Returns a Locale instance for the best-matching language tag using the lookup mechanism defined in RFC 4647.

final var locales = List.of(
        Locale.forLanguageTag("zh"),
        Locale.forLanguageTag("zh-Hant"),
        Locale.forLanguageTag("zh-Hant-TW"),
        Locale.forLanguageTag("en"),
        Locale.forLanguageTag("en-US")
);

{
    final var priorityList = List.of(
            new Locale.LanguageRange("en-US"),
            new Locale.LanguageRange("zh-Hant")
    );

    final var ret = Locale.lookup(priorityList, locales);
    System.out.println(ret.toLanguageTag()); // en-US
}
{
    final var priorityList = List.of(
            new Locale.LanguageRange("zh-Hant"),
            new Locale.LanguageRange("en-US")
    );

    final var ret = Locale.lookup(priorityList, locales);
    System.out.println(ret.toLanguageTag()); // zh-Hant
}
{
    final var priorityList = List.of(
            new Locale.LanguageRange("en-US-POSIX")
    );

    final var ret = Locale.lookup(priorityList, locales);
    System.out.println(ret.toLanguageTag()); // en-US
}

static String lookupTag (List<Locale.LanguageRange> priorityList, Collection<String> tags)

Returns the best-matching language tag using the lookup mechanism defined in RFC 4647.

final var tags = List.of(
        "zh",
        "zh-Hant",
        "zh-Hant-TW",
        "en",
        "en-US"
);

{
    final var priorityList = List.of(
            new Locale.LanguageRange("en-US"),
            new Locale.LanguageRange("zh-Hant")
    );

    final var ret = Locale.lookupTag(priorityList, tags);
    System.out.println(ret); // en-US
}
{
    final var priorityList = List.of(
            new Locale.LanguageRange("zh-Hant"),
            new Locale.LanguageRange("en-US")
    );

    final var ret = Locale.lookupTag(priorityList, tags);
    System.out.println(ret); // zh-Hant
}
{
    final var priorityList = List.of(
            new Locale.LanguageRange("en-US-POSIX")
    );

    final var ret = Locale.lookupTag(priorityList, tags);
    System.out.println(ret); // en-US
}

static void setDefault (Locale newLocale)

Sets the default locale for this instance of the Java Virtual Machine.

Please see getDefault().

static void setDefault (Locale.Category category, Locale newLocale)

Sets the default locale for the specified Category for this instance of the Java Virtual Machine.

Please see getDefault(Locale.Category category).

Locale stripExtensions ()

Returns a copy of this Locale with no extensions.

final var locale = Locale.forLanguageTag("ja-JP-u-ca-japanese-x-lvariant-JP");
System.out.println(locale.toLanguageTag()); // ja-JP-u-ca-japanese-x-lvariant-JP
System.out.println(locale.stripExtensions()); // ja_JP_JP
final var locale = Locale.US;
System.out.println(locale.toLanguageTag()); // en-US
System.out.println(locale.stripExtensions()); // en_US

String toLanguageTag ()

Returns a well-formed IETF BCP 47 language tag representing this locale.

final var us = Locale.US;
System.out.println(us.toLanguageTag()); // en-US

final var japan = Locale.JAPAN;
System.out.println(japan.toLanguageTag()); // ja-JP
final var locale = Locale.forLanguageTag("en-US-POSIX");
System.out.println(locale.toLanguageTag()); // en-US-POSIX

final String toString ()

Returns a string representation of this Locale object, consisting of language, country, variant, script, and extensions as below: language + "" + country + "" + (variant + "#" | "#") + script + "" + extensions Language is always lower case, country is always upper case, script is always title case, and extensions are always lower case.

This method is legacy. It is recommended to use the toLanguageTag method instead.


Related posts

To top of page