package com.wnl.demo;
import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;
import org.junit.Test;
public class Demo01 {
/**
* 文本国际化
*/
@Test
public void test1(){
ResourceBundle rb=ResourceBundle.getBundle("msg");
String greet=rb.getString("hello");
System.out.println(greet);
}
@Test
public void test2(){
Locale locale=Locale.US;//之地国家地区
//如果文件里面没有locale指定的国家或者地区,则使用本地平台默认的语言
ResourceBundle rb=ResourceBundle.getBundle("msg",locale);
String greet=rb.getString("hello");
System.out.println(greet);
}
/**
* 日期国际化
*/
@Test
public void test3(){
Locale locale=Locale.CHINA;
Date date=new Date();
/*getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale)
* 第一个DateFormat.FULL:日期格式
* 第二个DateFormat.FULL:时间格式
* locale:区域
*/
DateFormat df=DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale);
String s1=df.format(date);
System.out.println(s1);
//使用默认
DateFormat df2=DateFormat.getDateTimeInstance();
String s2=df2.format(date);
System.out.println(s2);
}
/**
* 数字国际化
*/
@Test
public void test4(){
int money=1000;
Locale inLocale=Locale.CHINA;
NumberFormat nf1=NumberFormat.getCurrencyInstance(inLocale);
String s1=nf1.format(money);
System.out.println(s1);
//设置百分比
NumberFormat nf2=NumberFormat.getPercentInstance(inLocale);
// nf2.setMinimumFractionDigits(4);//设置小数位数最小值 0.0300%
nf2.setMaximumFractionDigits(6);//设置小数位数最大值
float intrest=0.0003f;
String s2=nf2.format(intrest);
System.out.println(s2);
}
/**
* 批量格式化
*/
@Test
public void test5(){
Locale locale=Locale.CHINA;
String s="在{0,time,short},英国剑桥大学拒绝了{1}个中国学生的雅思成绩凭证,导致{2}中国学生重新考试";
Object obj[]=new Object[]{new Date(),1000,680};
MessageFormat mf=new MessageFormat(s,locale);
String s1=mf.format(obj);
System.out.println(s1);
}
}
Java国际化
最新推荐文章于 2025-05-11 22:52:31 发布
