/* * [TestLocale.java] * * Summary: Display the Locale/Currency acquired in various ways. * * Copyright: (c) 2011-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2011-08-21 initial version * 1.1 2011-08-23 add Lew Bloch's requested extensions, display of user.country and setDefault. * 1.2 2011-10-01 add currency */ package com.mindprod.example; import java.text.NumberFormat; import java.util.Currency; import java.util.Locale; import static java.lang.System.*; /** * Display the Locale/Currency acquired in various ways. * * @author Roedy Green, Canadian Mind Products * @version 1.2 2011-10-01 add currency * @since 2011-08-21 */ public final class TestLocale { /** * display fields of a Locale * * @param locale the Locale * @param desc description of the Locale */ private static void showLocale( final String desc, final Locale locale ) { out.println( desc ); out.println( "display:" + locale.getDisplayName() ); out.println( "country:" + locale.getCountry() ); out.println( "ISO3:" + locale.getISO3Country() ); out.println( "display country:" + locale.getDisplayCountry() ); out.println( "language:" + locale.getLanguage() ); out.println( "display language:" + locale.getDisplayLanguage() ); out.println( "user.country:" + System.getProperty( "user.country", "n/a" ) ); out.println( "user.language:" + System.getProperty( "user.language", "n/a" ) ); final Currency currency = Currency.getInstance( locale ); out.println( "currency code:" + currency.getCurrencyCode() ); out.println( "currency numeric code:" + currency.getNumericCode() ); NumberFormat cf = NumberFormat.getCurrencyInstance( locale ); out.println( "currency format: " + cf.format( 9.99d ) ); out.println(); } /** * Display current Locale * * @param args not used */ public static void main( String[] args ) { // various ways to get a Locale // demonstrates bugs in Windows 7 64-bit JDK 1.7 when country is configured in control panel as Canada. // user.country reports as US instead of CA // Locale.getDefault.getCountry() reports US instead of CA Locale defaultLocale = Locale.getDefault(); // browser/JVM default showLocale( "D E F A U L T", defaultLocale ); Locale specifiedLocale = new Locale( "en", "US" ); // language/country showLocale( "E N U S", specifiedLocale ); Locale localeConstant = Locale.CANADA_FRENCH; // static final constants showLocale( "C A N A D A _ F R E N C H", localeConstant ); Locale.setDefault( Locale.CANADA ); Locale forcedDefault = Locale.getDefault(); // Note, this does not change the user.country property showLocale( "F O R C E D C A N A D A D E F A U L T", forcedDefault ); // Locale serverLocale = request.getLocale(); // used in a Servlet to get remote user's locale } }