JSP国际化的相关API

来源:互联网 发布:软件代理商 编辑:程序博客网 时间:2024/06/04 00:48

本章已纯代码演示


package test;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 {@Testpublic void testLocale(){Locale locale=Locale.CHINA;//zh_CNlocale.toString();//CNlocale.getCountry();//zhlocale.getLanguage();//中国locale.getDisplayCountry();//中文locale.getDisplayLanguage();//中文(中国)locale.getDisplayName();//得到所有国家简写 (数组)locale.getISOCountries();//得到所有语言简写 (数组)locale.getISOLanguages();//得到所有类型Locale对象  (Locale数组)locale.getAvailableLocales();}@Testpublic void testDateFormat() throws ParseException{//创建一个LocaleLocale locale=Locale.CHINA;//创建一个DateFormat,设置时间格式DateFormat df=DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM,locale);//当前系统的时间Date date=new Date();//转换String d=df.format(date);//2017-4-9 15:29:39System.out.println(d);}@Testpublic void testNumberFormat(){//创建一个LocaleLocale locale=Locale.CHINA;//一个double类型double d=89757.78;//格式化数字NumberFormat nf1=NumberFormat.getNumberInstance(locale);String s1=nf1.format(d);//89,757.78System.out.println(s1);//格式化货币NumberFormat nf2=NumberFormat.getCurrencyInstance(locale);String s2=nf2.format(d);//¥89,757.78System.out.println(s2);}@Testpublic void testMessageFormat(){//设置占位符String msg="{0}{1}{2}";//...可变参数(可变数组)  String s=MessageFormat.format(msg,"你","是","猪");//打印  你是猪System.out.println(s);}@Testpublic void testResourceBundle(){//创建一个LocaleLocale locale=Locale.CHINA;//创建一个ResourceBundleResourceBundle resourceBundle=ResourceBundle.getBundle("i18n", locale);//打印:用户名如果是Locale.US  打印:nameSystem.out.println(resourceBundle.getString("uname"));}}

0 0