JSP国际化的相关API

本章已纯代码演示


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 {
	@Test
	public void testLocale(){
		Locale locale=Locale.CHINA;
		//zh_CN
		locale.toString();
		//CN
		locale.getCountry();
		//zh
		locale.getLanguage();
		//中国
		locale.getDisplayCountry();
		//中文
		locale.getDisplayLanguage();
		//中文(中国)
		locale.getDisplayName();
		//得到所有国家简写 (数组)
		locale.getISOCountries();
		//得到所有语言简写 (数组)
		locale.getISOLanguages();
		//得到所有类型Locale对象  (Locale数组)
		locale.getAvailableLocales();
	}
	@Test
	public void testDateFormat() throws ParseException{
		//创建一个Locale
		Locale 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:39
		System.out.println(d);
	}
	@Test
	public void testNumberFormat(){
		//创建一个Locale
		Locale locale=Locale.CHINA;
		//一个double类型
		double d=89757.78;
		//格式化数字
		NumberFormat nf1=NumberFormat.getNumberInstance(locale);
		String s1=nf1.format(d);
		//89,757.78
		System.out.println(s1);
		//格式化货币
		NumberFormat nf2=NumberFormat.getCurrencyInstance(locale);
		String s2=nf2.format(d);
		//¥89,757.78
		System.out.println(s2);
	}
	@Test
	public void testMessageFormat(){
		//设置占位符
		String msg="{0}{1}{2}";
		//...可变参数(可变数组)  
		String s=MessageFormat.format(msg,"你","是","猪");
		//打印  你是猪
		System.out.println(s);
	}
	
	@Test
	public void testResourceBundle(){
		//创建一个Locale
		Locale locale=Locale.CHINA;
		//创建一个ResourceBundle
		ResourceBundle resourceBundle=ResourceBundle.getBundle("i18n", locale);
		//打印:用户名		如果是Locale.US  打印:name
		System.out.println(resourceBundle.getString("uname"));
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值