当前位置: 移动技术网 > IT编程>开发语言>Java > javaweb 国际化:DateFormat,NumberFormat,MessageFormat,ResourceBundle的使用

javaweb 国际化:DateFormat,NumberFormat,MessageFormat,ResourceBundle的使用

2019年07月22日  | 移动技术网IT编程  | 我要评论

javaweb 国际化

dateformat:格式化日期的工具类,本身是一个抽象类;

numberformat:格式化 数字 到 数字字符串,或货币字符串的字符类;

messageformat: 可以格式化模式字符串,模式字符串: 带占位符的字符串: "date: {0}, salary: {1}",可以通过 format 方法会模式字符串进行格式化

resourcebundle:资源包类,在类路径(src)下需要有对应的资源文件: basename.properties. 其中 basename 是基名;

文件名为:test_zh_cn.properties,文件为:date=\u65e5\u671f,salary=\u5de5\u8d44

文件名为: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 {
  
  /**
   * resourcebundle: 资源包类.
   * 
   * 1. 在类路径下需要有对应的资源文件: basename.properties. 其中 basename 是基名.
   * 2. 可以使用 基名_语言代码_国家代码.properties 来添加不同国家或地区的资源文件. i18n_zh_cn.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("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); 
  }
  
  /**
   * messageformat: 可以格式化模式字符串
   * 模式字符串: 带占位符的字符串: "date: {0}, salary: {1}"
   * 可以通过 format 方法会模式字符串进行格式化
   */
  @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); 
  }
  
  /**
   * numberformat: 格式化数字到数字字符串, 或货币字符串的工具类
   * 1. 通过工厂方法获取 numberformat 对象
   * numberformat.getnumberinstance(locale); //仅格式化为数字的字符串
   * numberformat.getcurrencyinstance(locale); //格式为货币的字符串
   * 
   * 2. 通过 format 方法来进行格式化
   * 3. 通过 parse 方法把一个字符串解析为一个 number 类型. 
   */
  @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);
    
  }
  
  /*
   * 7. 若有一个字符串, 如何解析为一个 date 对象呢 ? 
   * i. 先创建 dateformat 对象: 创建 dateformat 的子类 simpledateformat 对象
   * simpledateformat(string pattern). 
   * 其中 pattern 为日期, 时间的格式, 例如: yyyy-mm-dd hh:mm:ss
   * ii. 调用 dateformat 的 parse 方法来解析字符串到 date 对象. 
  */
  @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); 
  }
  
  /**
   * dateformat: 格式化日期的工具类. 
   * dateformate 本身是一个抽象类. 
   * 
   * 1. 若只希望通过 dateformat 把一个 date 对象转为一个字符串, 则可以通过 dateformat 的工厂方法来获取 dateformat 对象
   * 2. 可以获取只格式化 date 的 dateformat 对象: getdateinstance(int style, locale alocale) 
   * 3. 可以获取只格式化 time 的 dateformat 对象: gettimeinstance(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 对象到字符串. 
   */
  @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); 
    
  }

  /**
   * locale: java 中表示国家或地区的类. jdk 中提供了很多常量.
   * 也可以通过 locale(languagecode, countrycode) 的方式来创建 
   * 在 web 应用中可以通过 request.getlocale() 方法来获取. 
   */
  @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国际化的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网