DateFormat和MessageFormat

MessageFormat(动态问文本)

一.如果一个字符串中包含了多个与国际化相关的数据,可以使用MessageFormat类对这些数据进行批量处理。

例如:At 12:30 pm on jul 3,1998, a hurricancedestroyed 99 houses and caused $1000000 of damage

•   以上字符串中包含了时间、数字、货币等多个与国际化相关的数据,对于这种字符串,可以使用MessageFormat类对其国际化相关的数据进行批量处理。

MessageFormat 类如何进行批量处理呢?

•   MessageFormat类允许开发人员用占位符替换掉字符串中的敏感数据(即国际化相关的数据)。

•   MessageFormat类在格式化输出包含占位符的文本时,messageFormat类可以接收一个参数数组,以替换文本中的每一个占位符。

二.   模式字符串:

•   On {0}, a hurricance destroyed {1} houses and caused {2} of damage.

MessageFormat类

•   MessageFormat(Stringpattern) 

实例化MessageFormat对象,并装载相应的模式字符串。

•   format(objectobj[])

格式化输出模式字符串,参数数组中指定占位符相应的替换对象。

l  format(new Object[ ]{date, new Integer(99),new Double(1E7) })

例题讲解:package com.hbsi.test;

 

import java.text.MessageFormat;

import java.util.Date;

import java.util.Locale;

import java.util.ResourceBundle;

 

public class Demo3 {

 

   /**

    * @param args

    */

   public static void main(String[] args) {

     // TODO Auto-generatedmethod stub

     //模式字符串

     String pattern="At{0,time,full}on{0,date,long}, a hurricance destroyed {1,number} houses and caused{2,number,currency} of damage.";

     MessageFormat mf=new MessageFormat(pattern,Locale.US);

     //准备参数数组

     Object [] objs={new Date(),new Integer(99),new Double(1e7)};

     //执行格式化

     String result=mf.format(objs);

     System.out.println(result);

     //加入到资源文件中

    

     ResourceBundle bundle=ResourceBundle.getBundle("com.hbsi.resource.MyResource",Locale.ENGLISH);

     result=bundle.getString("title");

     //System.out.println(result);

     MessageFormat mf1=new MessageFormat(result,Locale.ENGLISH);

     System.out.println(mf.format(objs));

    

   }

 

}

三.模式字符串与占位符

l 占位符有三种方式书写方式:

•   {argumentIndex}: 0-9 之间的数字,表示要格式化对象数据在参数数组中的索引号

•   {argumentIndex,formatType}: 参数的格式化类型

•   {argumentIndex,formatType,FormatStyle}: 格式化的样式,它的值必须是与格式化类型相匹配的合法模式、或表示合法模式的字符串。

代码分析:

String pattern = "At {0, time, short} on {0,date}, a destroyed'\n'"

              +"{1} houses and caused {2, number, currency} of damage.";

MessageFormat msgFmt = new MessageFormat(pattern,Locale.US);

String datetimeString = "Jul 3, 1998 12:30PM";

Date date = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,

DateFormat.SHORT,Locale.US).parse(datetimeString);

String event = "a hurricance";

Object []msgArgs = {date, event, newInteger(99), new Double(1E7)};

String result = msgFmt.format(msgArgs);

System.out.println(result);

 

Date Format类(国际化日期)

一.          DateFormat类可以将一个日期/时间对象格式化为表示某个国家地区的日期/时间字符串。

二.          DateFormat类除了可按国家地区格式化输出日期外,它还定义了一些用于描述日期/时间的显示模式的 int 型的常量,包括FULL, LONG, MEDIUM, DEFAULT, SHORT,实例化DateFormat对象时,可以使用这些常量,控制日期/时间的显示长度。

三.          例如,对于日期/时间“2005年9月15日下午4时41分20秒”,如设置了DateFormat的显示模式,则日期/时间显示样式分别为:

l SHORT模式完全是数字的,在中文环境下显示为“05-9-15 下午4:41”;在英文环境下为“9/15/05 4:41 PM”。

l MEDIUM模式比SHORT模式长些,在中文环境显示为“2005-9-15 16:41:20”;在英文环境下显示为“Sep 15, 2005 4:41:20 PM”。

l LONG模式比MEDIUM模式更长一些,在中文环境下显示为“2005年9月15日 下午04时41分20秒”;在英文环境下显示为“September 15, 2005 4:41:20 PM CST”。

l FULL模式指定日期/时间的完整格式,在中文环境下显示为“2005年9月15日 星期四 下午04时41分20秒 CST”;在英文环境下,这个日期/时间显示为“Thursday, September 15, 2005 4:41:20 PM CST”。

四.          实例化DateFormat类有九种方式,以下三种为带参形式,下面列出的三种方式也可以分别不带参,或只带显示样式的参数。

l getDateInstance(intstyle, Locale aLocale):以指定的日期显示模式和本地信息来获得DateFormat实例对象,该实例对象不处理时间值部分。

l getTimeInstance(intstyle, Locale aLocale):以指定的时间显示模式和本地信息来获得DateFormat实例对象,该实例对象不处理日期值部分。

l getDateTimeInstance(intdateStyle, int timeStyle, Locale aLocale):以单独指定的日期显示模式、时间显示模式和本地信息来获得DateFormat实例对象。

五.          DateFormat对象的方法:

format: 将date对象格式化为符合某个本地环境习惯的字符串。

parse:将字符串解析为日期/时间对象

l 注意:parse和format完全相反,一个是把date时间转化为相应地区和国家的显示样式,一个是把相应地区的时间日期转化成date对象,该方法在使用时,解析的时间或日期要符合指定的国家、地区格式,否则会抛异常。

六.          DateFormat对象通常不是线程安全的,每个线程都应该创建自己的 DateFormat  实例对象

NumberFormat类

一.NumberFormat 可以将一个数值格式化为符合某个国家地区习惯的数值字符串,也可以将符合某个国家地区习惯的数值字符串解析为对应的数值

NumberFormat 类的方法:

•   format 方法:将一个数值格式化为符合某个国家地区习惯的数值字符串

•   parse 方法:将符合某个国家地区习惯的数值字符串解析为对应的数值。

二.            实例化NumberFormat类时,可以使用locale对象作为参数,也可以不使用,下面列出的是使用参数的。

l getNumberInstance(Localelocale):以参数locale对象标识的本地信息来获得具有多种用途的NumberFormat实例对象

l getIntegerInstance(Localelocale):以参数locale对象所标识的本地信息来获得处理整数的NumberFormat实例对象

l getCurrencyInstance(Localelocale):以参数locale对象所标识的本地信息来获得处理货币的NumberFormat实例对象

l getPercentInstance(Localelocale):以参数locale对象所标识的本地信息来获得处理百分比数值的NumberFormat实例对象

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值