Mega Code Archive

 
Categories / Java Book / 003 Essential Classes
 

0212 Locale class

The Locale class describes a geographical or cultural region. The Locale class defines the following constants that are useful for dealing with the most common locales: CANADA CANADA_FRENCH CHINA CHINESE ENGLISH FRANCE FRENCH GERMAN GERMANY ITALIAN ITALY JAPAN JAPANESE KOREA KOREAN PRC SIMPLIFIED_CHINESE TAIWAN TRADITIONAL_CHINESE UK US For example, the expression Locale.CANADA represents the Locale object for Canada. The constructors for Locale are Locale(String language) Locale(String language, String country) Locale(String language, String country, String data) These constructors build a Locale object to represent a specific language and in the case of the last two, country. These values must contain ISO-standard language and country codes. static void setDefault(Locale localeObj) This sets the default locale to that specified by localeObj. final String getDisplayCountry( ) return human-readable strings that can be used to display the name of the country final String getDisplayLanguage( ) return human-readable strings that can be used to display the name of the language final String getDisplayName( ) return human-readable strings that can be used to display the name of the complete description of the locale The default locale can be obtained using getDefault( ), shown here: static Locale getDefault( ) DateFormat and SimpleDateFormat depend on the locale. The following code shows how to bind Locale.GERMANY to NumberFormat. /*Output: * User's number (GERMANY): 1.976,083 * */ import java.text.NumberFormat; import java.util.Locale; public class MainClass { public static void main(String args[]) throws Exception { NumberFormat numberFormat = NumberFormat.getInstance(); numberFormat.setParseIntegerOnly(false); double usersNumber = 1976.0826; numberFormat = NumberFormat.getNumberInstance(Locale.GERMANY); System.out.println("User's number (GERMANY): " + numberFormat.format(usersNumber)); } }