Javaweb 国际化
DateFormat:格式化日期的工具类,本身是一个抽象类;
NumberFormat:格式化 数字 到 数字字符串,或货币字符串的字符类;
MessageFormat: 可以格式化模式字符串,模式字符串: 带占位符的字符串: "Date: {0}, Salary: {1}",可以通过 format 方法会模式字符串进行格式化
ResourceBundle:资源包类,在类路径(src)下需要有对应的资源文件: baseName.properties. 其中 baseName 是基名;
文件名为:test_zh_CN.properties,文件为:date=u65E5u671F,salary=u5DE5u8D44
文件名为:test_en_US.properties,文件为:date=date,salary=salary
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 testResourceBundle(){
Locale locale = Locale.CHINA;
ResourceBundle resourceBundle = ResourceBundle.getBundle("test", locale);
System.out.println(resourceBundle.getString("date"));
System.out.println(resourceBundle.getString("salary"));
String dateLabel = resourceBundle.getString("date");
String salLabel = resourceBundle.getString("salary");
String str = "{0}:{1}, {2}:{3}";
Date date = new Date();
double sal = 12345.12;
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
String dateStr = dateFormat.format(date);
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
String salStr = numberFormat.format(sal);
String result = MessageFormat.format(str, dateLabel, dateStr, salLabel, salStr);
System.out.println(result);
}
@Test
public void testMessageFormat(){
String str = "Date: {0}, Salary: {1}";
Locale locale = Locale.CHINA;
Date date = new Date();
double sal = 12345.12;
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
String dateStr = dateFormat.format(date);
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
String salStr = numberFormat.format(sal);
String result = MessageFormat.format(str, dateStr, salStr);
System.out.println(result);
}
@Test
public void testNumberFormat() throws ParseException{
double d = 123456789.123d;
Locale locale = Locale.FRANCE;
//
NumberFormat numberFormat = NumberFormat.getNumberInstance(locale);
String str = numberFormat.format(d);
System.out.println(str);
NumberFormat numberFormat2 = NumberFormat.getCurrencyInstance(locale);
str = numberFormat2.format(d);
System.out.println(str);
str = "123 456 789,123";
d = (Double) numberFormat.parse(str);
System.out.println(d);
str = "123 456 789,12 ?";
d = (Double) numberFormat2.parse(str);
System.out.println(d);
}
@Test
public void testDateFormat2() throws ParseException{
String str = "1990-12-12 12:12:12";
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = dateFormat.parse(str);
System.out.println(date);
}
@Test
public void testDateFormat(){
Locale locale = Locale.US;
Date date = new Date();
System.out.println(date);
//获取 DateFormat 对象
DateFormat dateFormat =
DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM, locale);
String str = dateFormat.format(date);
System.out.println(str);
}
@Test
public void testLocale(){
Locale locale = Locale.CHINA;
System.out.println(locale.getDisplayCountry());
System.out.println(locale.getLanguage());
locale = new Locale("en", "US");
System.out.println(locale.getDisplayCountry());
System.out.println(locale.getLanguage());
}
}
以上就是对Java web国际化的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!



