javaWEB国际化:DateFormat,NumberFormat,MessageFormat,ResourceBundle的使用
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());
} }
javaWEB国际化:DateFormat,NumberFormat,MessageFormat,ResourceBundle的使用的更多相关文章
- JavaWeb国际化
软件的国际化: 软件在不同的地方,适应不同的风格: 中国: 显示中文,以及服务符合中国习惯的文本字符串! 美国: 显示英文,以及服务符合他国习惯的文本字符串! 这种软件,就叫国际化的软件! 如何做到国 ...
- 国际化,java.util.ResourceBundle使用详解
java.util.ResourceBundle使用详解 一.认识国际化资源文件 这个类提供软件国际化的捷径.通过此类,可以使您所编写的程序可以: 轻松地本地化或翻译成不同的 ...
- javaweb 国际化
国际化又称为 i18n:internationalization 软件实现国际化,需具备哪些特征:对于程序中固定使用的文本元素,例如菜单栏.导航条等中使用的文本元素.或错误提示信息,状态信息等,需要根 ...
- javaWEB国际化(jsp中使用)
在jsp页面中使用国际化方法,首先将jstl开源架包:jstl.jar,standard.jar导进去 并在src目录下建立以test开头,.properties结尾的文件:test_en_US.pr ...
- 11.javaweb国际化标签库
一.国际化标签库 1,格式化标签库提供的标签 2,标签详解 2.1<fmt:setLocale>标签 下面设置不同的区域,并在设置的区域下显示日期 2.2<fmt:requestEn ...
- 国际化之fmt标签
1. 什么是国际化和本地化: I. 本地化:一个软件在某个国家或地区使用时,采用该国家或地区的语言,数字,货币,日期等习惯.II. 国际化:软件开发时,让它能支持多个国家和地区的本地化应用.使得应用软 ...
- Java WEB 笔记
1. 部署并启动 tomcat 服务器 1). 解压 apache-tomcat-version 到一个非中文目录下 2). 配置一个环境变量,JAVA_HOME(指向 JDK 安装目录)或 JRE_ ...
- JavaWeb 后端 <十三> 之 监听器 JSTL国际化
1. 监听器 1.1 概述 监听器: 主要是用来监听特定对象的创建或销毁.属性的变化的! 是一个实现特定接口的普通java类! 对象: 自己创建自己用 (不用监听) 别人创建自己用 (需要监听) ...
- JavaWeb的国际化
国际化 1.国际化开发概述 1.1.软件的国际化 软件开发时,要使它能同时应对世界不同地区和国家的方法,并针对不同地区和国家的方法,提供相应的,符合来访者阅读习惯的页面或数据 国际化简称:i18n : ...
随机推荐
- crsctl stat res -t 和 crsctl stat res -init -t
11.2.0.2的grid infrastructure中crsctl stat res命令不再显示如ora.cssd.ora.ctssd.ora.diskmon等基础资源的信息.但是查看这些基础资源 ...
- asp.net mvc 后台怎么接受前端返回的array list dictionary
参考了别人的文章,我这样尝试去写: 数据源:memberInRoles var memberInRoles= {}; for(var i=0;i<sureOptions.length;i++){ ...
- FileInputStream and FileOutputStream
Java FileOutputStream class Java FileOutputStream is an output stream for writing data to a file. If ...
- (转)@SuppressWarnings的使用、作用、用法
在java编译过程中会出现很多警告,有很多是安全的,但是每次编译有很多警告影响我们对error的过滤和修改,我们可以在代码中加上 @SuppressWarnings(“XXXX”) 来解决 例如:@S ...
- Java: xml转换
java对于xml的转换有很多种,比较有名的有:DOM, DOM4J, JDOM, SAX.这里要介绍的是javax.xml包的对xml文件的转换.相比于前面几种是最简单的. 直接上代码: Stude ...
- ajax常用参数
url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址.前台跳转到后台 请求参数:前台向后台传数据 回调函数:回调函数就是一个自定义的函数在发生特定的事件的时候调用来处理这个事件 ...
- JS获取项目根目录
function getRootPath(){ //获取当前网址,如: http://localhost:8088/test/test.jsp var curPath=window.document. ...
- java文件下载 rest
/** * 返回文件二进制 * */ @GET @Path("/excel") @Produces("application/vnd.ms-excel; charset= ...
- SLC、MLC和TLC三者的区别
SLC=Single-LevelCell,即1bit/cell,速度快寿命长,价格超贵(约MLC3倍以上的价格),约10万次擦写寿命 MLC=Multi-LevelCell,即2bit/cell,速度 ...
- 《zw版·Halcon-delphi系列原创教程》 2d照片-3d逆向建模脚本
<zw版·Halcon-delphi系列原创教程> 2d照片-3d逆向建模脚本 3D逆向建模,是逆向工程的核心要素. 3D逆向建模,除了目前通用的3D点云模式,通过2D图像实现 ...