12.2.1 The Locale Class The java.util.Locale class is (Geocities web hosting)
12.2.1 The Locale Class The java.util.Locale class is undeniably the most important I18N class in the Java library. Almost all of the support for internationalization and localization in or around the Java language relies on this class. The Locale class provides Java with instances of the locale concept mentioned earlier. A particular instance of the Locale represents a unique language and region. When a class in the Java library modifies its functionality during runtime based on a Locale object, it’s said to be locale-sensitive. For example, the java.text.DateFormat is locale-sensitive because it will format a date differently depending on a particular Locale object. The Locale objects don’t do any of the I18N formatting or parsing work. They are used as identifiers by the locale-sensitive classes. When you acquire an instance of the DateFormat class, you can pass in a Localeobject for the United States. The DateFormatclass does all of the locale-sensitive parsing and formatting; it relies on the Locale only to identify the proper format. Be very careful when using the java.text.Format class or any of its descendants, including DateFormat, NumberFormat, and SimpleDateFormat, because they are not thread-safe. The thread-safety problem exists because an instance of the Calendar class is stored as a member variable and accessed during the parse( )and format( ) method invocations. You will need to use a separate instance for each thread or synchronize access externally. Don’t store a single instance in somewhere like the application scope and allow multiple client threads to access it. You can, however, store instances in the users’ sessions and use different instances for each user to help ensure thread-safety. The thread-safety problem includes all versions of Java, including 1.4. The API documentation for the Format classes has been updated to indicate the known design issue. When you create a Locale object, you typically specify the language and country code. The following code fragment illustrates the creation of two Locale objects, one for the U.S. and the other for Great Britain: Locale usLocale = new Locale(”en”, “US”); Locale gbLocale = new Locale(”en”, “GB”); The first argument in the constructor is the language code. The language code consists of two lowercase letters and must conform to the ISO-639 specification. You can find a complete listing of the available language codes at http://www.unicode.org/unicode/onlinedat/languages.html. The second argument is the country code. It consists of two uppercase letters that must conform to the ISO-3166 specification. The list of available country codes is available at http://www.unicode.org/unicode/onlinedat/countries.html. The Locale class provides several static convenience constants that allow you to acquire an instance of the most-often-used locales. For example, to get an instance of a Japanese locale, you could use either of these:
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.