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

Source: Internet
Author: User
Tags dateformat

DateFormat: A tool class that formats a date, itself an abstract class;

NumberFormat: A character class that formats numeric to numeric strings, or currency strings;

Messageformat: Can format pattern string, pattern string: string with placeholder: "Date: {0}, Salary: {1}", can be formatted using the Format method of the pattern string

ResourceBundle: Resource bundle class, requires a corresponding resource file under the Classpath (SRC): basename.properties. Where BaseName is the base name;

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

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

ImportJava.text.DateFormat;ImportJava.text.MessageFormat;ImportJava.text.NumberFormat;Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;ImportJava.util.Locale;ImportJava.util.ResourceBundle;Importorg.junit.Test; Public classI18ntest {/*** 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.      The key for all resource files that require the same base name must be exactly the same. * 4. You can use the NATIVE2ASCII command to get an ASC code for a Chinese character pair. Eclipse built-in tool * 5. You can call ResourceBundle's Getbundle (base name, Locale instance) to get the ResourceBundle object * 6.      You can call ResourceBundle's getString (key) to get the value of the value string for the resource file. * 7.      Combined with DateFormat, NumberFormat, Messageformat can achieve internationalization. *      */@Test Public voidTestresourcebundle () {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=NewDate (); DoubleSal = 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 pattern String * Pattern string: string with placeholder: "Date: {0}, Salary: {1}" * Can be formatted using the Format method of the pattern string. /c1>*/@Test Public voidTestmessageformat () {String str= "Date: {0}, Salary: {1}"; Locale Locale=Locale.china; Date Date=NewDate (); DoubleSal = 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: Formatting numbers to numeric strings, or tool classes for currency strings * 1. Get the NumberFormat object by factory method * Numberformat.getnumberinstance (LO Cale); A string formatted as a number * numberformat.getcurrencyinstance (locale); The format is the currency of the string * * 2. Formatted with the Format method * 3.      Parses a string into a number type by using the Parse method. */@Test Public voidTestnumberformat ()throwsparseexception{DoubleD = 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 there is a string, how to parse it into a Date object?      * I. Create DateFormat object: Create DateFormat Subclass SimpleDateFormat Object * SimpleDateFormat (String pattern). * Where pattern is a date, the format of time, for example: Yyyy-mm-dd HH:MM:SS * ii.      Call DateFormat's Parse method to parse the string into the Date object. */@Test Public voidTESTDATEFORMAT2 ()throwsparseexception{String str= "1990-12-12 12:12:12"; DateFormat DateFormat=NewSimpleDateFormat ("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 DateFormat Object * 2 through DateFormat's factory method. You can get a DateFormat object that only formats 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 DateFormat objects that both format Date and format time: * getdatetimeinstance (int datestyle, int timestyle, Locale alocale) * 5. Where style can be evaluated as: DATEFORMAT constant: Short, MEDIUM, LONG, full. Locale is the locale object representing the national region * 6.      Formats a Date object into a string by using the format method of DateFormat. */@Test Public voidTestdateformat () {locale locale=locale.us; Date Date=NewDate ();                 SYSTEM.OUT.PRINTLN (date); //get DateFormat ObjectDateFormat DateFormat =dateformat.getdatetimeinstance (Dateformat.long, Dateformat.medium, locale); String Str=Dateformat.format (date);             System.out.println (str); }    /*** Locale:java represents the country or region of the class.     Many constants are available in the JDK.      * can also be created through the Locale (Languagecode, CountryCode) method can be obtained through the Request.getlocale () way in the WEB application. */@Test Public voidTestlocale () {locale locale=Locale.china;        System.out.println (Locale.getdisplaycountry ());                 System.out.println (Locale.getlanguage ()); Locale=NewLocale ("en", "US");        System.out.println (Locale.getdisplaycountry ());     System.out.println (Locale.getlanguage ()); }    }

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

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.