Javaweb internationalization: The use of Dateformat,numberformat,messageformat,resourcebundle _java

Source: Internet
Author: User
Tags dateformat locale java web

Javaweb Internationalization

DateFormat: A tool class that formats dates and is itself an abstract class;

NumberFormat: the character class that formats numbers into numeric strings, or currency strings;

Messageformat: You can format pattern strings, pattern strings: strings with placeholders: "Date: {0}, Salary: {1}", which can be formatted by using the Format method with a pattern string

ResourceBundle: Resource Bundle class, the corresponding resource file is required under the Classpath (SRC): basename.properties. Wherein BaseName is the base name;

File name: test_zh_cn.properties, file is: date=\u65e5\u671f,salary=\u5de5\u8d44

File name: test_en_us.properties, file is: date=date,salary=salary

Import Java.text.DateFormat;
Import Java.text.MessageFormat;
Import Java.text.NumberFormat;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import Java.util.Locale;

Import Java.util.ResourceBundle;

Import Org.junit.Test;
   public class I18ntest {/** * ResourceBundle: Resource Bundle class. * * 1. A corresponding resource file is required under the classpath: Basename.properties.
   Where BaseName is the base name. * 2. You can use the base name _ language code _ Country code. Properties to add resource files for different countries or regions. I18n_zh_cn.properties * 3. 
   Requires that the key for all resource files with the same base name be exactly the same. * 4. You can use the NATIVE2ASCII command to get the ASC code for one of the Chinese characters. Eclipse has a built-in tool * 5. You can call the Getbundle (base name, Locale instance) of ResourceBundle to get the ResourceBundle object * 6. 
   You can call ResourceBundle's getString (key) to get the value of the resource file's values string. * 7. 
   Combined with DateFormat, NumberFormat, Messageformat can achieve internationalization. 
    * */@Test public void Testresourcebundle () {Locale Locale = Locale.china;
  
    ResourceBundle ResourceBundle = resourcebundle.getbundle ("Test", locale); System.out.println (resourcebundle.getstring ("DatE "));
    
    System.out.println (resourcebundle.getstring ("salary"));
    String Datelabel = resourcebundle.getstring ("date");
    
    String Sallabel = resourcebundle.getstring ("salary");
    
    String str = "{0}:{1}, {2}:{3}";
    Date date = new Date ();
    
    Double sal = 12345.12;
    DateFormat DateFormat = dateformat.getdateinstance (dateformat.medium, locale);
    
    String datestr = Dateformat.format (date);
    NumberFormat NumberFormat = numberformat.getcurrencyinstance (locale);
    
    String salstr = Numberformat.format (SAL);
    String result = Messageformat.format (str, Datelabel, DATESTR, Sallabel, SALSTR); 
  SYSTEM.OUT.PRINTLN (result);
   /** * Messageformat: Can format the pattern String * Pattern string: string with placeholder: "Date: {0}, Salary: {1}" * Can be formatted by using the Format method with a pattern string
    
    */@Test public void Testmessageformat () {String str = "Date: {0}, Salary: {1}";
    Locale Locale = Locale.china;
    Date date = new Date ();
    
    Double sal = 12345.12; DateFormat daTeformat = Dateformat.getdateinstance (dateformat.medium, locale);
    
    String datestr = Dateformat.format (date);
    NumberFormat NumberFormat = numberformat.getcurrencyinstance (locale);
    
    String salstr = Numberformat.format (SAL);
    String result = Messageformat.format (str, DATESTR, SALSTR); 
  SYSTEM.OUT.PRINTLN (result); /** * NumberFormat: Format numeric to numeric string, or currency string tool class * 1. Obtain the NumberFormat object * numberformat.getnumberinstance (locale) through the factory method; Strings formatted as numbers * numberformat.getcurrencyinstance (locale); String formatted as Currency * * 2. format by using the Format method of * 3. 
   Resolves a string to a number type by using the Parse method.
    */@Test public void Testnumberformat () throws parseexception{double d = 123456789.123d;
    
    Locale Locale = locale.france;
    
    NumberFormat NumberFormat = numberformat.getnumberinstance (locale);
    String str = Numberformat.format (d); 
    
    System.out.println (str);
  NumberFormat numberFormat2 = numberformat.getcurrencyinstance (locale);  str = Numberformat2.format (d); 
    
    System.out.println (str);
    str = "123 456 789,123";
    D = (Double) numberformat.parse (str); 
    
    System.out.println (d);
    str = "123 456 789,12?";
    D = (Double) numberformat2.parse (str);
    
  System.out.println (d); } * * 7. 
   If you have a string, how do you parse it into a Date object? 
   * I. Create a DateFormat object: Create a DateFormat Subclass SimpleDateFormat Object * SimpleDateFormat (String pattern). * Where pattern is the date, the time format, for example: Yyyy-mm-dd hh:mm:ss * II. 
  Call the DateFormat parse method to resolve the string to the Date object.
    * * @Test public void TestDateFormat2 () throws parseexception{String str = "1990-12-12 12:12:12";
    
    DateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");
    Date date = Dateformat.parse (str); 
  SYSTEM.OUT.PRINTLN (date); 
   /** * DateFormat: A tool class that formats dates. 
   * Dateformate itself is an abstract class. * * 1. If you only want to convert a Date object to a string by DateFormat, you can get the DateFormat object * 2 through the DateFormat factory method. You can get a DateFormat object that formats only Date: GetdateinStance (int style, Locale alocale) * 3. You can get the DateFormat object that only formats time: gettimeinstance (int style, Locale alocale) * 4.  You can get a DateFormat object that formats Date and also formats time: * getdatetimeinstance (int datestyle, int timestyle, Locale alocale) * 5. Where style can take a value of: DateFormat constants: Short, MEDIUM, LONG, full. Locale is the Locale object representing the country area * 6. 
   Format a Date object to a string by using the DateFormat format method.
    
    * * @Test public void Testdateformat () {Locale Locale = locale.us;
    Date date = new Date (); 
    
    SYSTEM.OUT.PRINTLN (date);  Gets the DateFormat object DateFormat dateformat = Dateformat.getdatetimeinstance (Dateformat.long, DateFormat.MEDIUM,
    locale);
    String str = dateformat.format (date); 
    
  System.out.println (str); A class that represents a country or region in the/** * Locale:java.
   Many constants are provided in the JDK. 
   * can also be created by means of Locale (Languagecode, CountryCode), which can be obtained through the Request.getlocale () method in WEB applications.
    * * @Test public void Testlocale () {Locale Locale = Locale.china; System.out.println(Locale.getdisplaycountry ()); 
    
    System.out.println (Locale.getlanguage ());
    locale = new locale ("en", "US");
    System.out.println (Locale.getdisplaycountry ()); 
  System.out.println (Locale.getlanguage ());

 }
  
}

Above is the Java Web internationalization of the data collation, follow-up continue to supplement the relevant information, thank you for your support for this site!

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.