国际化-----Java

  • Locale
    Locale :Java 中表示国家或地区的类,JDK 中提供很多常量,也可以通过Locale(language,countryCode) 的方式来创建,在WEB 应用中可以通过request.getLocale() 方法来获取.
/*locale :Java 中表示国家或地区的类,JDK 中提供很多常量
               也可以通过locale(language,countryCode) 的方式来创建
                在WEB 应用中可以通过request.getLocale()方法来获取、*/
        Locale locale = Locale.CHINA;// 2019-3-2 2:03:08
        // locale = locale.US;//Mar 2, 2019 2:02:20 AM
        Date date = new Date();
        System.err.println(date);
        // 获取dateFormat 对象
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
        System.out.println(dateFormat.format(date));

DateFormat :

  1. 可以获取只格式化 DateDateFormat 对象。getDateInstance(int style,Locale locale).
  2. 获取只格式化 TimeDateFormat 对象,getTimeInstance(int style, Locale locale).
  3. 获取既格式化 Date 又格式化 TimeDateFormat 对象,getDateTimeInstance(int style, int timeStyle,Locale locale).
  4. 其中Style 可以取值: DateFoemat 的常量:SHORT,MEDIUM,LONG,FULL,Locale 则代表国家地区的 Locale .
 String dateStr = "1980-12-20 15:35:58";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date2 = simpleDateFormat.parse(dateStr);
        System.out.println(date2);//Sat Dec 20 15:35:58 CST 1980

  • 数字国际化
    NumberFormat: 格式化数字到数字字符串,或货币字符串的工具,
    NumberFormat.getNumberInstance(locale)、、格式化数字的字符串。
    NumberFormat.getCurrencytInstance(Locale)、、格式化货币字符串
ouble d = 1234567.789d;
        Locale locale2 = Locale.CHINA;
        NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale2);
        System.out.println(numberFormat.format(d));//¥1,234,567.79
        locale2 = Locale.US;
        numberFormat = NumberFormat.getCurrencyInstance(locale2);
        System.out.println(numberFormat.format(d));//$1,234,567.79

  • MessageFormat:
    可以格式化字符串,模式字符串:使用占位符 “{0},看看:{1}”
    Date:2019-3-2,Salary: ¥123.44
String meString = "Date:{0},Salary: {1}";
        Locale locale3 = Locale.CHINA;
        Date date3 = new Date();
        double da = 123.445;
        dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale3);
        String format = dateFormat.format(date);
        numberFormat = NumberFormat.getCurrencyInstance(locale3);
        String format2 = numberFormat.format(da);
        String format3 = MessageFormat.format(meString, format, format2);
        System.out.println(format3);

  • ResourceBundle 类
    ResourceBundle 类用来描述一个资源包,一个资源包包含一组与某个本地环境相关的对象,可以从资源包中获取特定的本地环境的对象,对于不停的本地环境,可以有不同的ResourceBundle对象与之关联,关联的ResourceBundle 对象中包含该本地环境下专有的对象。
    ResourceBundle :资源包
  1. 在类路径下需要有对应的资源文件,basename.properties .其中basename 是基名。
  2. 可以使用 基名_语言代码_国家代码.peoperties 来添加不同国家或地区的资源文件。i18n_zh_CN.properties,i18n_en_US.properties
  3. 要求所有的基名相同的资源文件的Key 必须完全一致。
  4. 可以使用native2asci 命令来得到汉字.对一个的asc 码,eclipse 内置。
  5. 可以调用ResourceBundlegetBundle(基名,locale 实例) 获取 ResourceBundle 对象。
  6. 可以调用 ResourceBundle 的getString(key) 来获取资源文件的value 字符串的值。
  7. 结合DateFormat,NumberFormat,MessageFormat 即可实现国际化
Locale locale = Locale.US;
        ResourceBundle resourceBundle = ResourceBundle.getBundle("i18n", locale);
        System.out.println(resourceBundle.getString("date"));
        System.out.println(resourceBundle.getString("brithday"));

        Locale locale1 = Locale.CHINA;
        ResourceBundle resourceBundle1 = ResourceBundle.getBundle("i18n", locale1);
        System.out.println(resourceBundle1.getString("date").getBytes("utf-8"));
        System.out.println(resourceBundle1.getString("brithday"));

国际化-----Java