Java uses the getDefault () method and setDefault () method of the Locale object to obtain the local country code settings. The GetSetLocale class is defined in the code. Let's talk about the reason for internationalization. International programs can run on different operating systems. Before determining whether to implement a certain function in a local method, you need to determine the local object Locale, or to display a piece of text in a specific region code, you need to set the default region as the given region code. The specific implementation method in this example:
1. the GetSetLocale class only includes the min () method. In this method, the local country code settings are obtained and printed, and the local default country code is set according to the command line parameters, set the date and number display format based on the default country code.
2. Compile the GetSetLocale class with the following code:
The code is as follows: |
Copy code |
Import java. text .*; Import java. util .*; Public class GetSetLocale { Public static void main (String [] args ){ // Obtain and print the local country code settings for the province. Locale l = Locale. getDefault (); System. out. println ("Today's Locale is" + l ); // Set the local default country code according to the command line parameters Switch (args. length ){ Case 0: Locale. setDefault (Locale. TRADITIONAL_CHINESE ); Break; Case 1: Throw new IllegalArgumentException (); Case 2: Locale. setDefault (new Locale (args [0], args [1]); Break; Default: System. out. println ("Usage: SetLocale [language [country]"); } // Set the date and number display format based on the default country code. DateFormat df = DateFormat. getInstance (); NumberFormat nf = NumberFormat. getInstance (); System. out. println ("Locale set to" + Locale. getDefault (). getDisplayCountry () + Locale. getDefault (). getDisplayLanguage ()); System. out. println (df. format (new Date ())); System. out. println (nf. format (123.4567 )); } } |