ResourceBundle
ResourceBundle class is used to store text and objects which are locale-sensitive.
Generally, we use property files to store locale-specific text and then represent them using a ResourceBundle object.
―Given two properties files:
Messages_en_US.properties containing
message=Hello world!
Messages_fr_FR.properties containing
message=Bonjour le monde!
―Given the resource bundle:
ResourceBundle bundle = ResourceBundle.getBundle("Messages", Locale.US);―We can use the resource bundle to get value in the selected language (here French or English).
import java.util.Locale; import java.util.ResourceBundle; public class LocalizationFrUSTest { public static void main(String[] args) { ResourceBundle bundle = ResourceBundle.getBundle("Messages", Locale.US); System.out.println("Message in "+Locale.US +": "+bundle.getString("message")); //Message in en_US: Hello world! bundle = ResourceBundle.getBundle("Messages", Locale.FRANCE); System.out.println("Message in "+Locale.FRANCE +": "+bundle.getString("message")); //Message in fr_FR: Bonjour le monde! } }
Locale class
―Define:
A Locale class object represents a specific geographical/political/cultural region.
Any operation requiring a Locale to perform its task is called locale-sensitive operation,
and uses the Locale to master information relative to the user.
Locale contains: Language, Script, Country, Variant, Extensions
―Snippet:
public class LocaleTest{ public static void main(String[] args) { Locale l=Locale.getDefault(); //Default Locale Properties: out(l.getDisplayCountry()); //United States out(l.getDisplayLanguage()); //English out(l.getDisplayName()); //English (United States) out(l.getISO3Country()); //USA out(l.getISO3Language()); //eng out(l.getLanguage()); //en out(l.getCountry()); //US Locale frL = new Locale("fr","fr"); //fr Locale Properties: out(frL.getDisplayCountry()); //France out(frL.getDisplayLanguage()); //French out(frL.getDisplayName()); //French (France) out(frL.getISO3Country()); //FRA out(frL.getISO3Language()); //fra out(frL.getLanguage()); //fr out(frL.getCountry()); //FR } static void out(String s) { System.out.println(s); } }
Display setting
―Define:
It is possible to independently set the default locale for the display setting that is used in menus and dialogs. Introduced in the Java SE 7 release, the
Locale.getDefault(Locale.Category)
method takes a Locale.Category parameter. Passing the DISPLAY enum returns the default locale used by the UI.
//Locale.getDefault(Locale.Category.DISPLAY);
The corresponding
setDefault(Locale.Category, Locale)
method allows setting the locale for the desired category.
//Locale.setDefault(Locale.Category.DISPLAY, new Locale("fr", "FR"));
The no-argument getDefault method returns the DISPLAY default value.
//Locale.getDefault();
On the Windows platform, these default values are initialized according to the settings in the Windows control panel.
―Snippet:
public class LocaleTest{ public static void main(String[] args) { Locale defaultLocale = Locale.getDefault(); System.out.println(defaultLocale.getDisplayLanguage()); //English Locale frLocale = new Locale("fr", "FR"); System.out.println(defaultLocale.getDisplayLanguage(frLocale)); //anglais } }
NumberFormat
―Given:
static void out(Object str) { System.out.println(str); }
―We have
Locale da = new Locale("da", "DK"); NumberFormat fmt = NumberFormat.getInstance(da); out(fmt.format(100.76)); // 100,76 fmt = NumberFormat.getInstance(en); out(fmt.format(100.76)); // 100.76 // CURRENCIES fmt = NumberFormat.getCurrencyInstance(da); out(fmt.format(100.76)); // kr 100,76 fmt = NumberFormat.getCurrencyInstance(en); out(fmt.format(100.76)); // $100.76 // PERCENTAGE fmt = NumberFormat.getPercentInstance(en); out(fmt.format(0.76)); // 76% // DIGIT fmt = NumberFormat.getInstance(en); fmt.setMinimumIntegerDigits(2); fmt.setMaximumIntegerDigits(3); fmt.setMinimumFractionDigits(2); fmt.setMaximumFractionDigits(3); out(fmt.format(12234.763443)); // 234.763 // ROUNDING fmt = NumberFormat.getInstance(en); fmt.setMinimumFractionDigits(0); fmt.setMaximumFractionDigits(0); out(fmt.format(99.50)); // 100 fmt.setRoundingMode(RoundingMode.HALF_DOWN); out(fmt.format(99.50)); // 99 // PARSING fmt = NumberFormat.getInstance(da); out(fmt.parse("100,76")); // 100,76 fmt = NumberFormat.getInstance(en); out(fmt.parse("100,76")); // 10076
DecimalFormat
―Given

and
static String pattern = ""; static DecimalFormat fmt = new DecimalFormat(pattern); static void out(Object str) { System.out.println(str); } static void set(DecimalFormat fmt, String pattern) { Test.fmt = new DecimalFormat(pattern); Test.fmt.applyPattern(pattern); } static void set2(DecimalFormat fmt, String pattern) { Test.fmt = fmt; Test.fmt.applyPattern(pattern); }
―We have
out("INTRO"); set(fmt, "####,####.##"); double nb = 123456789.123; out(nb); // 1.23456789123E8 out(fmt.format(nb)); // 1,2345,6789.12 out("PATTERN"); // pattern ###.### set(fmt, "###.###"); out(fmt.format(nb)); // 123456789.123 // pattern ###.# set(fmt, "###.#"); out(fmt.format(nb)); // 123456789.1 // pattern ###,###.## set(fmt, "###,###.##"); out(fmt.format(nb)); // 123,456,789.12 // pattern 000.### nb = 9.34; set(fmt, "000.###"); out(fmt.format(nb)); // 009.34 out("LOCALE"); set(fmt, "###.##"); nb = 123.45; Locale en = new Locale("en", "US"); Locale da = new Locale("da", "DK"); fmt = (DecimalFormat) NumberFormat.getNumberInstance(en); set2(fmt, "###.##"); out(fmt.format(nb)); // 123.45 fmt = (DecimalFormat) NumberFormat.getNumberInstance(da); set2(fmt, "###.##"); out(fmt.format(nb)); // 123,45 out("SEPARATOR"); set(fmt, "#,###.###"); nb = 126473.4567; out(fmt.format(nb)); // 126,473.457 DecimalFormatSymbols sep = new DecimalFormatSymbols(); sep.setDecimalSeparator(';'); sep.setGroupingSeparator(':'); fmt = new DecimalFormat(pattern, sep); out(fmt.format(nb)); // 126:473;457 out("GROUPING"); set(fmt, ""); fmt.setGroupingSize(2); out(fmt.format(nb)); //12,64,73.4567
DateFormat
java.text.DateFormat class formats dates as per the locale. As different countries use different formats to display dates. This class is extremely useful in dealing with dates in the internationalization of applications.
// DATE Locale loc = new Locale("da", "DK"); DateFormat fmt = DateFormat.getDateInstance(); out(fmt.format(new Date())); // Mar 2, 2020 fmt = DateFormat.getDateInstance(DateFormat.DEFAULT, loc); out(fmt.format(new Date())); // 2. mar. 2020 // PATTERNS fmt = DateFormat.getDateInstance(DateFormat.DEFAULT); out(fmt.format(new Date())); // Mar 2, 2020 fmt = DateFormat.getDateInstance(DateFormat.SHORT); out(fmt.format(new Date())); // 3/2/20 fmt = DateFormat.getDateInstance(DateFormat.MEDIUM); out(fmt.format(new Date())); // Mar 2, 2020 fmt = DateFormat.getDateInstance(DateFormat.LONG); out(fmt.format(new Date())); // March 2, 2020 fmt = DateFormat.getDateInstance(DateFormat.FULL); out(fmt.format(new Date())); // Monday, March 2, 2020 // TIME fmt = DateFormat.getTimeInstance(DateFormat.DEFAULT); out(fmt.format(new Date())); // 4:12:49 AM fmt = DateFormat.getTimeInstance(DateFormat.SHORT); out(fmt.format(new Date())); // 4:12 AM fmt = DateFormat.getTimeInstance(DateFormat.MEDIUM); out(fmt.format(new Date())); // 4:12:49 AM fmt = DateFormat.getTimeInstance(DateFormat.LONG); out(fmt.format(new Date())); // 4:12:49 AM CET fmt = DateFormat.getTimeInstance(DateFormat.FULL); out(fmt.format(new Date())); // 4:12:49 AM Central European Standard Time // DATE & TIME fmt = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT); out(fmt.format(new Date())); // Mar 2, 2020, 4:14:57 AM fmt = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); out(fmt.format(new Date())); // 3/2/20, 4:14 AM fmt = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); out(fmt.format(new Date())); // Mar 2, 2020, 4:14:57 AM fmt = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); out(fmt.format(new Date())); // March 2, 2020 at 4:14:57 AM CET fmt = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL); out(fmt.format(new Date())); // Monday, March 2, 2020 at 4:14:57 AM Central European Standard Time
SimpleDateFormat
java.text.SimpleDateFormat class formats dates as per the given pattern. It is also used to parse dates from a string where the string contains a date in the mentioned format.
―Given

and
static void out(Object str) { System.out.println(str); }
―We have
// INTRO String ptn = "dd-MM-yyyy"; SimpleDateFormat fmt = new SimpleDateFormat(ptn); // Mon Mar 02 06:27:27 CET 2020 Date d = new Date(); out(d); out(fmt.format(d)); // 02-03-2020 String dateText = "29-11-2017"; d = fmt.parse(dateText); out(fmt.format(d)); // 29-11-2017 // LOCALE Locale locale = new Locale("da", "DK"); ptn = "EEEEE MMMMM yyyy"; fmt = new SimpleDateFormat(ptn); // Mon Mar 02 06:27:27 CET 2020 d = new Date(); out(d); out(fmt.format(d)); // Monday March 2020 fmt = new SimpleDateFormat(ptn, locale); out(fmt.format(d)); // mandag marts 2020 // PATTERN ptn = "dd-MM-yy"; fmt = new SimpleDateFormat(ptn); d = new Date(); out(fmt.format(d)); // 02-03-20 ptn = "MM-dd-yyyy"; fmt = new SimpleDateFormat(ptn); out(fmt.format(d)); // 03-02-2020 ptn = "yyyy-MM-dd HH:mm:ss"; fmt = new SimpleDateFormat(ptn); out(fmt.format(d)); // 2020-03-02 06:27:27 ptn = "EEEEE MMMMM yyyy HH:mm:ss.SSSZ"; fmt = new SimpleDateFormat(ptn); out(fmt.format(d)); // Monday March 2020 06:27:27.765+0100