Obtain the local country code using the Locale. getDefault () method in Java

Source: Internet
Author: User
Tags dateformat locale

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 ));
 }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.