栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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国际化的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/149250.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号