Java Study Notes-internationalization (41), java Study Notes

Source: Internet
Author: User
Tags dateformat

Java Study Notes-internationalization (41), java Study Notes

Internationalization: internationalization is I18N.

Example:

Websites of undergraduate colleges and universities generally have two webpage styles: Chinese and English. Therefore, this method of displaying different page styles based on different user groups is called page internationalization.

Translation VS Internationalization

Translation: Chrome

Internationalization: It mainly refers to the internationalization of page display information, and other resources such as files cannot be internationalized.

1. Internationalization Principle

Hello = \ u4F60 \ u597D

Greet_en.properties

Hello = Hello

2. Implement the following java class

Public static void main (String [] args) {// obtain the local region information Locale locale = Locale. US; // bind resource ResourceBundle bundler = ResourceBundle. getBundle ("cn. itcast. ps. greet ", locale); System. out. println (bundler. getString ("hello "));}

Summary:

If you do not have a designated editor, you can directly use native2ascii.exe to transcode Chinese characters.

Internationalization Variables

1 Date Internationalization

The DateFormat class displays different date formats in different regions.

Format and parse date data based on region information.

Static DateFormat getDateInstance (int style, Locale aLocale) à get date instance

Static DateFormat getTimeInstance (int style, Locale aLocale) à get time instance

Static DateFormat getDateTimeInstance (int dateStyle, int timeStyle, Locale aLocale) à get date and time

Date parse (String source) à parsing Date

String format (Object obj) à format date

Example:

Get date and time

Long January 21, 2013 04:16:41 P.M. January 21,201 3 4:17:27 pm cstfull January 21, 2013 Monday 04:18:19 P.M. CSTMonday, January 21,201 3 4:18:37 PM CSTMEDIUM2013-1-21 16: 19: 33Jan 21,201 3 4:19:45 PMSHORT13-1-21/21/13 PM

Exercise: Resolve the following string of characters to a date object.

String date = "January 21, 2013"; resolved as a date object.

Long Short

Public static void main (String [] args) throws Exception {// I18NDate (); String date = "January 21, 2013"; Locale locale = Locale. CHINA; DateFormat format = DateFormat. getDateTimeInstance (DateFormat. LONG, DateFormat. SHORT, locale); Date new_date = format. parse (date); System. out. println (new_date );}

Digital globalization

NumberFormat

Static NumberFormat getInstance (Locale inLocale) International General Digital static NumberFormat getCurrencyInstance (Locale inLocale) International Currency static NumberFormat getPercentInstance (Locale inLocale) International percentage

Internationalization

String format(Object obj)Number parse(String source)

Example:

Public static void main (String [] args) throws Exception {// obtain the object Locale locale = Locale. CHINA; NumberFormat format = NumberFormat. getInstance (locale); String str = format. formats (123456.789); System. out. println (str); locale = Locale. CHINA; format = NumberFormat. getCurrencyInstance (locale); str = format. formats (123456.789); System. out. println (str); locale = Locale. CHINA; format = NumberFormat. getPercentInstance (locale); str = format. formats (0.789); System. out. println (str );}

Exercise: Resolve String str = "¥123.7"; to a number.

public static void main(String[] args) throws Exception{        //I18NNumber();        String str = "¥123.7";        Locale locale = Locale.CHINA;         NumberFormat format = NumberFormat.getCurrencyInstance(locale);        Number num = format.parse(str);        System.out.println(num.doubleValue()*3);    }

Dynamic text Internationalization

public static void main(String[] args) throws Exception{      String str =     "On {0}, a hurricance destroyed {1} houses and caused {2} of damage.";      String date = "Jul 3, 1998 12:30 PM";         DateFormat format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, Locale.US);      Date new_date = format.parse(date);            Double money = new Double(1E7);      NumberFormat numfor = NumberFormat.getCurrencyInstance(Locale.CHINA);      String new_str = numfor.format(money);            Object[] os = new Object[]{new_date,new Integer(77),new_str}      MessageFormat msg_for = new MessageFormat(str,Locale.CHINA);      String for_str = msg_for.format(os);      System.out.println(for_str);    }

Each time the above dynamic text is internationalized, all the data that needs to be internationalized must be internationalized separately and then filled one by one. Therefore, it is cumbersome to use the following pattern string.

public static void main(String[] args) throws Exception{        String pattern = "At {0, time, short} on {0, date}, a hurricance destroyed "            + "{1} houses and caused {2, number, currency} of damage.";        String datetimeString = "Jul 3, 1998 12:30 PM";        DateFormat format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, Locale.US);        Date new_date = format.parse(datetimeString);                Object[] os = new Object[]{new_date,new Integer(77),new Double(1E7)};                MessageFormat msg_for = new MessageFormat(pattern,Locale.US);        String for_str = msg_for.format(os);        System.out.println(for_str);    }

 

Case 1: Internationalization of login
<body>     <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="in" %>     <in:setLocale value="${pageContext.request.locale}"/>     <in:setBundle basename="cn.itcast.login.greet" var="bund" scope="page"/>     <form action="">        <table align="center" border="1">           <tr>             <td><in:message bundle="${pageScope.bund}" key="name"/></td>             <td><input type="text" name="uname"></td>           </tr>           <tr>             <td><in:message bundle="${pageScope.bund}" key="psw"/></td>             <td><input type="text" name="uname"></td>           </tr>           <tr>             <td colspan="2">                 <input type="submit" value='<in:message bundle="${pageScope.bund}" key="submit"/>'>                 <input type="reset" value='<in:message bundle="${pageScope.bund}" key="reset"/>'>             </td>           </tr>        </table>     </form>  </body>

Later, struts International labels are required in Struts2.x.

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.