JAVA WEB开发——国际化标签
一、DateFormat类
1、 DateFormat 对象的方法:
(1)format: 将date对象格式化为符合某个本地环境习惯的字符串。
(2)parse:将字符串解析为日期/时间对象
注意:parse和format完全相反,一个是把date时间转化为相应地区和国家的显示样式,一个是把相应地区的时间日期转化成date对象,该方法在使用时,解析的时间或日期要符合指定的国家、地区格式,否则会抛异常。
2、 DateFormat 对象通常不是线程安全的,每个线程都应该创建自己的 DateFormat 实例对象
二、NumberFormat类
1、NumberFormat 可以将一个数值格式化为符合某个国家地区习惯的数值字符串,也可以将符合某个国家地区习惯的数值字符串解析为对应的数值
2、NumberFormat 类的方法:
(1)format 方法:将一个数值格式化为符合某个国家地区习惯的数值字符串
(2)parse 方法:将符合某个国家地区习惯的数值字符串解析为对应的数值。
A)实例化NumberFormat类时,可以使用locale对象作为参数,也可以不使用,下面列出的是使用参数的。
B)getNumberInstance(Locale locale):以参数locale对象标识的本地信息来获得具有多种用途的NumberFormat实例对象
C)getIntegerInstance(Locale locale):以参数locale对象所标识的本地信息来获得处理整数的NumberFormat实例对象
D)getCurrencyInstance(Locale locale):以参数locale对象所标识的本地信息来获得处理货币的NumberFormat实例对象
E)getPercentInstance(Locale locale):以参数locale对象所标识的本地信息来获得处理百分比数值的NumberFormat实例对象
NumberFormat nf1=NumberFormat.getPercentInstance(Locale.US);
double f1=0.26;
str=nf1.format(f1);
System.out.println(str);
str="$89.0";
double f2=nf.parse(str).doubleValue();
System.out.println(f2);
str="89%";
double f3=nf1.parse(str).doubleValue();
System.out.println(f3);
三、MessageFormat(动态文本)
1、如果一个字符串中包含了多个与国际化相关的数据,可以使用MessageFormat类对这些数据进行批量处理。
2、例如:At 12:30 pm on jul 3,1998, a hurricance destroyed 99 houses and caused $1000000 of damage
(1)以上字符串中包含了时间、数字、货币等多个与国际化相关的数据,对于这种字符串,可以使用MessageFormat类对其国际化相关的数据进行批量处理。
3、 MessageFormat 类如何进行批量处理呢?
(2)MessageFormat类允许开发人员用占位符替换掉字符串中的敏感数据(即国际化相关的数据)。
(3)MessageFormat类在格式化输出包含占位符的文本时,messageFormat类可以接收一个参数数组,以替换文本中的每一个占位符。
四、模式字符串与占位符
1、At 12:30 pm on jul 3,1998, a hurricance destroyed 99 houses and caused $1000000 of damage
2、 模式字符串:
At {0} on {1},a destroyed {2} houses and caused {3} of damage.
五、格式化模式字符串
1、模式字符串:
On {0}, a hurricance destroyed {1} houses and caused {2} of damage.
2、 MessageFormat类
(1)MessageFormat(String pattern)
实例化MessageFormat对象,并装载相应的模式字符串。
(2)format(object obj[])
格式化输出模式字符串,参数数组中指定占位符相应的替换对象。
format(new Object[ ]{date, new Integer(99), new Double(1E7) })
模式字符串:pattern=At{0},a hurricance destroyed {1} housers and cause
MessageFormat mf=new MessageFormat(pattern);
mf.format();
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);
public static void main(String[] args) {
// TODO Auto-generated method stub
String pattern="At {0,time,short} On {0, date,long},a hurricance destroyed {1,number} housers and cause {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.US);
result=bundle.getString("title");
//System.out.println(result);
MessageFormat mf1=new MessageFormat(result,Locale.US);
System.out.println(mf1.format(objs));
}
DateFormat类型
SimpleDateFormat类型
函数
<body>
${fn:toLowerCase("WWW.HBSI.COM") }<br/>
$(fn:toUpperCase("Www.hbSI.com")}<br/>
--------${fn:trim(" dsdf " )}-------<br/>
${"<h1>你好</h1>" }
${fn:escapeXml("<h1>你好</h1>")}<br/>
${fn:length("1234567") }<br/>
${fn:split("www.hbsi.com","" )}<br/>
${fn:indexOf("www.it315.org","t3") }
${fn:indexOf("www.it315.org","aaa")}
${fn:indexOf("www.it315.org","") }<br/>
${fn:contains("www.it315.org", "org") }
${fn:contains("www.it315.org", "Org") }
${fn:contains("", "") }<br/>
${fn:containsIgnoreCase("www.it315.org", "Org") }
${fn:containsIgnoreCase (fn:toUpperCase("www.it315.org"), fn:toUpperCase("Org")) }<br/>
${fn:startsWith("www.it315.org","it315") }
${fn:startsWith("www.it315.org","www") }
${fn:startsWith("www.it315.org","") }<br/>
${fn:endsWith("www.it315.org","it315") }
${fn:endsWith("www.it315.org","org") }
${fn:endsWith("www.it315.org","") }
</body>
运行结果:www.hbsi.com
$(fn:toUpperCase("Www.hbSI.com")}
--------dsdf-------
你好
<h1>你好</h1>
7
[Ljava.lang.String;@16c50af
l 5 l 0l-1
truel false ltrue
trueltrue
truel true l false l
lfalse trueltrue