package i18n;
import java.text.DateFormat;
import java.text.FieldPosition;
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 {
@Test
public void MessageFormat2(){
String str="Date:{0},Salary:{1}";
Locale locale=Locale.CHINA;
Date date=new Date ();
double sal=12345.12;
StringBuffer result=new StringBuffer();
FieldPosition fieldPosition=new FieldPosition(0);
MessageFormat messageFormat=new MessageFormat(str,locale);
result = messageFormat.format(date,result,fieldPosition);
System.out.println(result);
}
/*ResourceBundle:资源包
* 1.在类路径下需要有对应的资源文件:baseName.properties 其中 baseName是基名
* 2.可以使用基名_语言代码_国家代码.properties来添加不同国家或地区的资源文件 i18n_en_US.properties
* 3.要求所有基名相同的源文件的key一样、
* 4.可以使用native2ASCII命令来得到汉子的ASC码 Eclipse内置了工具
* 5.可以调用ResourceBundle的getBundle(基名,locale实例)获取ResourceBundle对象
* 6.可以调用ResourceBundle的getString(key)方法来获取资源文件的value字符串的值
* 7.结合DateFormat,NumberFormat,MessageFormat即可实现国家化
*
* */
@Test
public void testResourceBundle(){
Locale locale=Locale.CHINA;
ResourceBundle resourceBundle=ResourceBundle.getBundle("i18n",locale);
// System.out.println(resourceBundle.getString("date"));
// System.out.println(resourceBundle.getString("salary"));
String datelob=resourceBundle.getString("date");
String salarylob=resourceBundle.getString("salary");
String str="{0}:{1},{2}:{3}";
Date date=new Date() ;
double sal=12345.12;
DateFormat date1=DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
String str1=date1.format(date);
NumberFormat numb=NumberFormat.getCurrencyInstance(locale);
String str2=numb.format(sal);
String result = MessageFormat.format(str, datelob,str1,salarylob,str2);
System.out.println(result);//日期:2018-9-30,工资:¥12,345.12
}
// MessageFormat :可以格式化模式字符串
// 模式字符串:带占位符的字符串
// 可以通过format方法会对模式字符串进行格式化
@Test
public void MassageFromat(){
String str="Date:{0},Salary{1}";
Locale locale=Locale.CHINA;
Date date=new Date() ;
double sal=12345.12;
DateFormat date1=DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
String str1=date1.format(date);
NumberFormat numb=NumberFormat.getCurrencyInstance(locale);
String str2=numb.format(sal);
String result = MessageFormat.format(str, str1,str2);
System.out.println(result);
}
// NumberFormat:格式化数字到数字字符串 或货币字符串的工具类
// 1.通过工厂方式来进行格式化
//NumberFormat.getNumberInstance(locale) //仅化为数字的字符串
//NumberFormat.getCurrencyInstance(locale)//格式化为货币的字符串
//2.通过format方法来进行格式化
//3.通过parse方法把一个字符串解析为Number类型
@Test
public void NumberFromat() throws ParseException {
double d=123456789.123d;
Locale locale=Locale.FRANCE;
//Date date=new Date() ;
NumberFormat nb=NumberFormat.getNumberInstance(locale);
String str1=nb.format(d);
System.out.println(str1);
NumberFormat numb=NumberFormat.getCurrencyInstance(locale);
String str2=numb.format(d);
System.out.println(str2);
//字符串转化为Number类型
//1.23456789123E8
//1.2345678912E8
String str="123 456 789,123";
d=(double) nb.parse(str);
System.out.println(d);
String str3="123 456 789,12 €";
d=(double) numb.parse(str3);
System.out.println(d);
}
@Test
public void testDateFormat2() throws Exception{
String str="1997-02-11 12:12:12";
DateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date=dateformat.parse(str);
System.out.println(date);
}
// /*DateFormat 日期格式化的工具类
// *DateFormat 是一个抽象类
// *1.若只希望通过 DateFormat 把一个Date对象转化为一个字符串,则可以通过DateFormat工厂的方法来获取
// *DateFormat对象
// *
// *2.可以获取只格式化的Date的DateFormat对象:getDateFormat对象 getDateInstance(int style, Locale aLocale)
// *3.可以获取只格式化的Time的DateFormat对象:getDateFormat对象 getDateTimeInstance(int style, Locale aLocale)
// *4.可以获取即格式化Date,也格式化Time的DateFormat对象 :
// * getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale)
// *5. 其中style可以取之为DateFormat的常量 :SHORT,MEDIUM,LONG,FULL,Locale则为代表国家地区的Locale对象
// *6.通过DateFormat的format方法来格式化Date对像 到字符串
// *7.若有一个字符串 如何解析一个Date对象呢
// *I 。先创建DATEORMAT对象:创建DateFormat的子类 SimpleDateFormat的对象昂
// *SimpleDateFormat(String pattern)
// *其中 pattern为日期 时间的格式为 YYYY-MM-DD hh:mm:ss
// *II二调用DateFormat的parse方法来解析字符串到Date对象
@Test
public void testDateFormat(){
Locale locale=Locale.UK;
Date date=new Date();
System.out.println(date);
//获取DateFormat对象
DateFormat dateformat=DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM, locale);
String str=dateformat.format(date);
System.out.println(str);
}
//
// Locale:Java中表示国家或地区的类 JDK提供了恒多常量
// * 也可以通过Locale(languageCode,countryCode)的方式来创建
// * 在WEB应用中可已通过request.getLocale()方法来获取
// *
//
@Test
public void testLocale(){
Locale locale=Locale.CHINA;
System.out.println(locale.getCountry());//国家
System.out.println(locale.getLanguage());//语言
locale=new Locale("en","US");
System.out.println(locale.getCountry());
System.out.println(locale.getLanguage());
}
}
国际化(i18n)方法
猜你喜欢
转载自blog.csdn.net/qq_42676998/article/details/82908320
今日推荐
周排行