Locale defaultLocale = Locale.getDefault(); // get the default locale String countryName = defaultLocale.getDisplayCountry(); // get the country name System.out.println(countryName); // print the country name
Locale[] availableLocales = Locale.getAvailableLocales(); // get all available locales for (Locale locale : availableLocales) { String countryName = locale.getDisplayCountry(); // get the country name System.out.println(locale.toString() + " - " + countryName); // print the locale and country name }This code retrieves the localized name for the country represented by each available locale and prints it to the console along with the locale code. The output will include a list of all available locales and their associated country names. The package library for java.util.Locale is part of the standard Java Development Kit (JDK) and is included in the java.util package.