Java : Locale with Examples
Locale (Java SE 18 & JDK 18) API Examples.
You will find code examples on most Locale methods.
Summary
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
final var locale = Locale.CANADA;
System.out.println(locale.toLanguageTag()); // en-CA
static final Locale CANADA_FRENCH
final var locale = Locale.CANADA_FRENCH;
System.out.println(locale.toLanguageTag()); // fr-CA
static final Locale CHINA
final var locale = Locale.CHINA;
System.out.println(locale.toLanguageTag()); // zh-CN
static final Locale CHINESE
final var locale = Locale.CHINESE;
System.out.println(locale.toLanguageTag()); // zh
static final Locale ENGLISH
final var locale = Locale.ENGLISH;
System.out.println(locale.toLanguageTag()); // en
static final Locale FRANCE
final var locale = Locale.FRANCE;
System.out.println(locale.toLanguageTag()); // fr-FR
static final Locale FRENCH
final var locale = Locale.FRENCH;
System.out.println(locale.toLanguageTag()); // fr
static final Locale GERMAN
final var locale = Locale.GERMAN;
System.out.println(locale.toLanguageTag()); // de
static final Locale GERMANY
final var locale = Locale.GERMANY;
System.out.println(locale.toLanguageTag()); // de-DE
static final Locale ITALIAN
final var locale = Locale.ITALIAN;
System.out.println(locale.toLanguageTag()); // it
static final Locale ITALY
final var locale = Locale.ITALY;
System.out.println(locale.toLanguageTag()); // it-IT
static final Locale JAPAN
final var locale = Locale.JAPAN;
System.out.println(locale.toLanguageTag()); // ja-JP
static final Locale JAPANESE
final var locale = Locale.JAPANESE;
System.out.println(locale.toLanguageTag()); // ja
static final Locale KOREA
final var locale = Locale.KOREA;
System.out.println(locale.toLanguageTag()); // ko-KR
static final Locale KOREAN
final var locale = Locale.KOREAN;
System.out.println(locale.toLanguageTag()); // ko
static final Locale PRC
final var locale = Locale.PRC;
System.out.println(locale.toLanguageTag()); // zh-CN
static final char PRIVATE_USE_EXTENSION
final var ext = Locale.PRIVATE_USE_EXTENSION;
System.out.println(ext); // x
static final Locale ROOT
final var locale = Locale.ROOT;
System.out.println(locale.toLanguageTag()); // und
static final Locale SIMPLIFIED_CHINESE
final var locale = Locale.SIMPLIFIED_CHINESE;
System.out.println(locale.toLanguageTag()); // zh-CN
static final Locale TAIWAN
final var locale = Locale.TAIWAN;
System.out.println(locale.toLanguageTag()); // zh-TW
static final Locale TRADITIONAL_CHINESE
final var locale = Locale.TRADITIONAL_CHINESE;
System.out.println(locale.toLanguageTag()); // zh-TW
static final Locale UK
final var locale = Locale.UK;
System.out.println(locale.toLanguageTag()); // en-GB
static final char UNICODE_LOCALE_EXTENSION
final var ext = Locale.UNICODE_LOCALE_EXTENSION;
System.out.println(ext); // u
static final Locale US
final var locale = Locale.US;
System.out.println(locale.toLanguageTag()); // en-US
Constructors
Locale (String language)
This method is legacy. It is recommended to use the forLanguageTag method or Locale.Builder instead.
Locale (String language, String country)
This method is legacy. It is recommended to use the forLanguageTag method or Locale.Builder instead.
Locale (String language, String country, String variant)
This method is legacy. It is recommended to use the forLanguageTag method or Locale.Builder instead.
Methods
Object clone ()
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)
Please see clone().
static List<Locale> filter (List<Locale.LanguageRange> priorityList, Collection<Locale> locales)
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)
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)
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)
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)
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 ()
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 ()
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 ()
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)
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 ()
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)
Please see getDisplayCountry().
final String getDisplayLanguage ()
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)
Please see getDisplayLanguage().
final String getDisplayName ()
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)
Please see getDisplayName().
String getDisplayScript ()
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)
Please see getDisplayScript().
final String getDisplayVariant ()
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)
Please see getDisplayVariant().
String getExtension (char 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 ()
Please see getExtension(char key).
String getISO3Country ()
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 ()
Please see getISO3Country().
static String[] getISOCountries ()
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)
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 ()
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 ()
Please see getCountry().
String getScript ()
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 ()
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 ()
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)
Please see getUnicodeLocaleKeys().
String getVariant ()
final var locale = Locale.forLanguageTag("en-US-POSIX");
System.out.println(locale.toLanguageTag()); // en-US-POSIX
System.out.println(locale.getVariant()); // POSIX
boolean hasExtensions ()
Please see getExtension(char key).
int 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)
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)
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)
Please see getDefault().
static void setDefault (Locale.Category category, Locale newLocale)
Please see getDefault(Locale.Category category).
Locale stripExtensions ()
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 ()
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 ()
This method is legacy. It is recommended to use the toLanguageTag method instead.
Related posts
- API Examples