Java乔晓松-JavaWeb开发-国际化2

来源:互联网 发布:触摸屏软件制作 编辑:程序博客网 时间:2024/05/17 08:59

Tip8:DateFormat类(国际化日期)

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

 

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

 

Tip9:DateFormat类

例如,对于日期/时间“2005915日下午44120,如设置了DateFormat的显示模式,则日期/时间显示样式分别为:

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

 

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

 

LONG模式比MEDIUM模式更长一些,在中文环境下显示为“2005915日下午044120;在英文环境下显示为“September 15, 2005 4:41:20 PM CST”

 

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

Tip10:实例化DateFormat类

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

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

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

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

Tip11:DateFormat类

DateFormat 对象的方法:

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

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

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

 

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

Tip12:NumberFormat类

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

NumberFormat 类的方法:

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

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

 

Tip13:NumberFormat类

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

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

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

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

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

<fmt:formatDate>标签

<fmt:formatDate>标签用于对日期和时间按本地化信息进行格式化,或对日期和时间按化为JSP页面作者自定义的格式进行格式化。语法格式如下:

<fmt:formatDate value="date"     

                            [type="{time|date|both}"]

                            [dateStyle="{default|short|medium|long|full}"]

                            [timeStyle="{default|short|medium|long|full}"]

                            [pattern="customPattern"]

                            [timeZone="timeZone"]

                            [var="varName"]

                            [scope="{page|request|session|application}"] />

<fmt:parseDate>标签

     <fmt:parseDate>标签与<fmt: formatDate >标签的作用正好相反,它用于将一个表示日期和时间的字符串解析成java.util.Date实例对象.

语法1,没有标签体的情况:

    <fmt:parseDate value="dateString"

                            [type="time|date|both"]

                            [dateStyle="default|short|medium|long|full"]

                            [timeStyle="default|short|medium|long|full"]

                            [pattern="customPattern"]

                            [timeZone="timeZone"]

                            [parseLocale="parseLocale"]

                            [var="varName"]

                            [scope="{page|request|session|application}"] />

<fmt:formatNumber>标签

      <fmt:formatNumber>标签用于将数值、货币或百分数按本地化信息进行格式化,或者按JSP页面作者自定义的格式进行格式化。

没有标签体的情况:

<fmt:formatNumber value="numericValue"

                          [type="{number|currency|percent}"]

                          [pattern="customPattern"]

                          [currencyCode="currencyCode"]

                          [currencySymbol="currencySymbol"]

                          [groupingUsed="{true|false}"]

                          [maxIntegerDigits="maxIntegerDigits"]

                          [minIntegerDigits="minIntegerDigits"]

                          [maxFractionDigits="maxFractionDigits"]

                          [minFractionDigits="minFractionDigits"]

                          [var="varName"]

                          [scope="{page|request|session|application}"] />

<fmt:parseNumber>标签

<fmt:parseDate>标签与<fmt: formatNumber >标签的作用正好相反,它用于将一个按本地化方式被格式化后的数值、货币或百分数解析为数值

语法1,没有标签体的情况:

<fmt:parseNumber value="numericValue"   

                            [type="{number|currency|percent}"]

                            [pattern="customPattern"]

                            [parseLocale="parseLocale"]

                            [integerOnly="{true|false}"]

                            [var="varName"

                     [scope="{page|request|session|application}"] />

语法2,有标签体的情况,在标签体中指定要被解析的日期和/或时间值:

<fmt:parseDate [type="time|date|both"]                            [dateStyle="default|short|medium|long|full"]                     [timeStyle="default|short|medium|long|full"]

              [pattern="customPattern"]

              [timeZone="timeZone"]

              [parseLocale="parseLocale"]

              [var="varName"]                        [scope="{page|request|session|application}"]>

 

              date value to be parsed

 

</fmt:parseDate>

Tip14:MessageFormat(动态文本)

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

例如:

       At 12:30 pm on jul 3,1998, a hurricance destroyed 99 houses and caused $1000000 of damage

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

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

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

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

Tip15:模式字符串与占位符

 

At 12:30 pm on jul 3,1998, a hurricance destroyed 99 houses and caused $1000000 of damage

 

模式字符串:

      At {0} on {1},a destroyed {2} houses and caused {3} of damage.

Tip16:格式化模式字符串

模式字符串:

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

MessageFormat

MessageFormat(String pattern) 

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

format(object obj[])

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

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

Tip17:格式化模式字符串

String pattern = "On {0}, a hurricance destroyed {1} houses and caused "

                     + "{2} of damage.";

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

 

//准备参数数组

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

Date date = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,

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

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

 

//执行格式化操作

String result = msgFmt.format(msgArgs);

System.out.println(result);

Tip18:模式字符串与占位符

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

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

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

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

Tip19:模式字符串与占位符

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:30 PM";

Date date = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,

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

String event = "a hurricance";

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

 

String result = msgFmt.format(msgArgs);

System.out.println(result);

<fmt:param>标签

<fmt:param>标签用于为格式化文本串中的占位符设置参数值,它只能嵌套在<fmt:message>标签内使用。

 

语法1,用value属性指定参数值:

<fmt:param value="messageParameter" />

 

语法2,在标签体中指定参数的值的情况:

<fmt:param>

       body content

</fmt:param>