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

 更新时间:2016年09月06日 16:09:10   投稿:lqh  
本文主要介绍javaWEB国际化的知识,这里整理了详细的资料及实现代码,有兴趣的小伙伴可以参考下

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

相关文章

  • 详解Java前缀树Trie的原理及代码实现

    详解Java前缀树Trie的原理及代码实现

    Trie又被称为前缀树、字典树。Trie利用字符串的公共前缀来高效地存储和检索字符串数据集中的关键词,最大限度地减少无谓的字符串比较,其核心思想是用空间换时间。本文主要介绍了Trie的原理及实现,感兴趣的可以了解一下
    2022-11-11
  • 关于maven项目中使用BCrypt加密方式

    关于maven项目中使用BCrypt加密方式

    BCrypt是一种基于Blowfish加密算法的密码散列函数,用于安全存储和验证用户密码,它通过引入盐和工作因子增加计算复杂度,有效防止彩虹表攻击和破解,BCrypt具备适应性工作因子、成本参数调整、迭代哈希和密钥扩展等特点,被广泛应用于Web应用程序的安全性设计中
    2024-10-10
  • 线程池之newFixedThreadPool定长线程池的实例

    线程池之newFixedThreadPool定长线程池的实例

    这篇文章主要介绍了线程池之newFixedThreadPool定长线程池的实例,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • 详解Java去除json数据中的null空值问题

    详解Java去除json数据中的null空值问题

    这篇文章主要介绍了详解Java去除json数据中的null空值问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08
  • Spring实现HikariCP连接池的示例代码

    Spring实现HikariCP连接池的示例代码

    在SpringBoot 2.0中,我们使用默认连接池是HikariCP,本文讲一下HikariCP的具体使用,具有一定的参考价值,感兴趣的可以了解一下
    2021-08-08
  • spring boot如何使用POI读取Excel文件

    spring boot如何使用POI读取Excel文件

    本文主要介绍使用POI进行Excel文件的相关操作,涉及读取文件,获取sheet表格,对单元格内容进行读写操作,以及合并单元格的操作
    2021-08-08
  • 解决SpringBoot运行Test时报错:SpringBoot Unable to find

    解决SpringBoot运行Test时报错:SpringBoot Unable to find

    这篇文章主要介绍了SpringBoot运行Test时报错:SpringBoot Unable to find a @SpringBootConfiguration,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • 解决@DateTimeFormat格式化时间出错问题

    解决@DateTimeFormat格式化时间出错问题

    这篇文章主要介绍了解决@DateTimeFormat格式化时间出错问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • java集成开发SpringBoot生成接口文档示例实现

    java集成开发SpringBoot生成接口文档示例实现

    这篇文章主要为大家介绍了java集成开发SpringBoot如何生成接口文档的示例实现过程,有需要的朋友可以借鉴参考下,希望能够有所帮助
    2021-10-10
  • Java在指定路径上创建文件提示不存在解决方法

    Java在指定路径上创建文件提示不存在解决方法

    在本篇文章里小编给大家整理的是一篇关于Java在指定路径上创建文件提示不存在解决方法,有需要的朋友们可以参考下。
    2020-02-02

最新评论