- 浏览: 506563 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (278)
- java (41)
- 设计模式 (4)
- sql (10)
- webservice (2)
- spring (9)
- struts (6)
- struts2 (32)
- hibernate (27)
- Struts_hibernate_Spring整合 (4)
- Velocity (1)
- Servlet (9)
- JSP (6)
- javascript (19)
- jquery (10)
- ajax (4)
- html、xml (3)
- JDBC (2)
- JDK (6)
- mysql (2)
- oracle (11)
- SqlServer (1)
- DB2 (4)
- tool (7)
- linux (5)
- UML (1)
- eclipse (8)
- 执行文件 (1)
- 应用服务器 (4)
- 代码重构 (1)
- 日本語 (19)
- 交规 (1)
- office (9)
- firefox (1)
- net (1)
- 测试 (1)
- temp (6)
- 对日外包 (1)
- windows (1)
- 版本控制 (1)
- android (2)
- 项目管理 (1)
最新评论
一。
MessageFormat
提供了以与语言无关方式生成连接消息的方式。使用此方法构造向终端用户显示的消息。
MessageFormat
获取一组对象,格式化这些对象,然后将格式化后的字符串插入到模式中的适当位置。
注:
MessageFormat
不同于其他 Format
类,因为
MessageFormat
对象是用其构造方法之一创建的(而不是使用 getInstance
MessageFormat
本身不实现特定于语言环境的行为。特定于语言环境的行为是由所提供的模式和用于已插入参数的子格式来定义的。
单引号的时候 如 String a = "abc'{0}'abc"; 必须改成 String a = "abc''{0}''abc";
MessageFormat运行开发者输出文本中的变量的格式。它是一个强大的类,就像下面的例子展示的那样:
String message = "Once upon a time ({1,date}, around about {1,time,short}), there " + "was a humble developer named Geppetto who slaved for " + "{0,number,integer} days with {2,number,percent} complete user " + "requirements. "; Object[ ] variables = new Object[ ] { new Integer(4), new Date( ), new Double(0.21) } String output = MessageFormat.format( message, variables ); System.out.println(output);
隐藏在信息中的是描述输出的格式的一种短小的代码,范例的输出如下:
Once upon a time (Nov 3, 2002, around about 1:35 AM), there was a humble developer
named Geppetto who slaved for 4 days with 21% complete user requirements.
假如相同的信息需要被重复输出但是变量的值不同,那么创建一个MessageFormat对象并给出信息。下面是上面的例子的修正版:
//String output = MessageFormat.format(message, variables ); //变为: MessageFormat formatter = new MessageFormat(message); String output = formatter.format(variables);
除了可以处理日期、时间、数字和百分数外,MessageFormat也可以处理货币,运行更多的数字格式的控制并且答应指定ChoiceFormat。
MessageFormat是一个极好的类,它应该经常被使用但是现在还没有。它的最大的缺点是数据是被作为变量传递而不是一个Properties对
象。一个简单的解决办法是写一个封装类,它会预解析字符串为格式化的结果,将Properties的key转换为一个数组索引,顺序是
Properties.keys( )返回的顺序。
二。例子
<!-- Generated by javadoc (build 1.6.0-beta2) on Fri Mar 09 12:51:16 CST 2007 -->
第一个例子
使用静态的方法 MessageFormat.format
,它在内部创建一个只使用一次的
MessageFormat
:
int planet = 7; String event = "a disturbance in the Force"; String result = MessageFormat.format( "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.", planet, new Date(), event);
输出为:
At 12:30 PM on Jul 3, 2053, there was a disturbance in the Force on planet 7.
下面的例子创建了一个可以重复使用的 MessageFormat
实例:
int fileCount = 1273; String diskName = "MyDisk"; Object[] testArgs = {new Long(fileCount), diskName}; MessageFormat form = new MessageFormat( "The disk \"{1}\" contains {0} file(s)."); System.out.println(form.format(testArgs));
不同 fileCount
值的输出:
The disk "MyDisk" contains 0 file(s). The disk "MyDisk" contains 1 file(s). The disk "MyDisk" contains 1,273 file(s).
对于更复杂的模式,可以使用 ChoiceFormat
来生成正确的单数和复数形式:
MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0}."); double[] filelimits = {0,1,2}; String[] filepart = {"no files","one file","{0,number} files"}; ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart); form.setFormatByArgumentIndex(0, fileform); int fileCount = 1273; String diskName = "MyDisk"; Object[] testArgs = {new Long(fileCount), diskName}; System.out.println(form.format(testArgs));
不同的 fileCount
值的输出:
The disk "MyDisk" contains no files. The disk "MyDisk" contains one file. The disk "MyDisk" contains 1,273 files.
如上例所示,可以以编程方式来创建 ChoiceFormat
,或使用模式创建。有关更多信息,请参阅 ChoiceFormat
。
form.applyPattern( "There {0,choice,0#are no files|1#is one file|1<are {0,number,integer} files}.");
注:
从上面的例子可以看到,由 MessageFormat
中的
ChoiceFormat
所生成的字符串要进行特殊处理;'{' 的出现用来指示子格式,并导致递归。如果
MessageFormat
和 ChoiceFormat
都是以编程方式创建的(而不是使用字符串模式),那么要注意不要生成对其自身进行递归的格式,这将导致无限循环。
当一个参数在字符串中被多次解析时,最后的匹配将是解析的最终结果。例如,
MessageFormat mf = new MessageFormat("{0,number,#.##}, {0,number,#.#}"); Object[] objs = {new Double(3.1415)}; String result = mf.format( objs ); // result now equals "3.14, 3.1" objs = null; objs = mf.parse(result, new ParsePosition(0)); // objs now equals {new Double(3.1)}
同样,使用包含同一参数多个匹配项的模式对 MessageFormat 对象进行解析时将返回最后的匹配。例如,
MessageFormat mf = new MessageFormat("{0}, {0}, {0}"); String forParsing = "x, y, z"; Object[] objs = mf.parse(forParsing, new ParsePosition(0)); // result now equals {new String("z")}
同步
消息格式不是同步的。建议为每个线程创建独立的格式实例。如果多个线程同时访问一个格式,则它必须是外部同步的。
发表评论
文章已被作者锁定,不允许评论。
-
Java8 ,JDK1.8 新特性
2016-12-08 14:58 735一、接口的默认方法 Java 8允许我们给接口 ... -
Google Guava官方教程 学习
2016-12-05 17:43 315http://ifeve.com/google- ... -
Guava 相关内容(一)
2016-05-20 00:08 477一、Java 不可以变的集合 Guava学习笔记: ... -
poi excel 相关
2015-04-07 11:22 647一、poi excel 分组(group) ... -
java 相关问题(四)
2013-05-24 15:54 1201十九、Java中对Map(HashMap,TreeMap, ... -
apache-common
2013-01-09 10:47 1007... -
Java注释的写法
2012-11-16 15:02 751一. Java 文档 // 注释 ... -
正则表达式
2012-05-25 09:19 947编程的大量工作都是在处理字符串,如验证输入、查 ... -
java 相关问题(三)
2012-03-08 16:31 1471十三、java 实现 调用 打印机 代码详解 ... -
J2EE秘籍
2012-02-13 15:42 688转:http://zhufeng1981.iteye.com/ ... -
java 相关问题(二)
2011-08-02 15:47 1054七、ThreadLocal 详解 首先,Thre ... -
Apache Commons BeanUtils
2011-06-08 17:24 1518功能说明: 顾名思义,Bean Utility就是Bean小 ... -
java 相关问题(一)
2011-05-10 19:16 989一、 java Cloneable 详 ... -
java 读写 properties
2011-04-19 14:15 1177一、 /* * @(#)RWProper ... -
JMS API 中文版
2011-04-13 14:20 798转:http://www.iteye.com/to ... -
ant 教程
2011-04-12 23:56 1118一、ant 教程 1 Ant是什么? ... -
properties 文件中 定义内容 相关问题
2011-02-22 20:41 2226一、在 properties 文件中 定义{ } 会 ... -
java 线程
2011-02-10 17:07 892一、 Runnable、 Thread ... -
java.util.logging (不用log4j配置,自己写log文件)
2010-10-11 11:55 7398<!-- Generated by javadoc ( ... -
java 静态块 非静态块
2010-09-21 17:39 1379一。一个简单的例子 1. 所有静态的(无论其是变量 ...
相关推荐
java中MessageFormat格式化字符串的例子,包括NumberFormat,DateFormat,ChoiceFormat等
主要介绍了Java中的MessageFormat.format用法实例,本文先是讲解了MessageFormat的语法,然后给出了多个操作实例,需要的朋友可以参考下
主要介绍了Java利用MessageFormat实现短信模板的匹配,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
9.6.4 使用MessageFormat处理包含占位符的字符串 349 9.6.5 使用类文件代替资源文件 350 9.6.6 使用NumberFormat格式化数字 350 9.6.7 使用DateFormat格式化日期 352 9.7 本章小结 355 本章练习 355 第10章 ...
官方版本,亲测可用
官方版本,亲测可用
Ø java.text.MessageFormat:用于格式化带占位符的字符串。 为了实现程序的国际化,必须先提供程序所需要的资源文件。资源文件的内容是很多key-value对。其中key是程序使用的部分,而value则是程序界面的显示字符...
format(1)DataFormat(2)NumberFormat(3)ChoiceFormat(4)MessageFormat调用方法:format.for
对于如何处理此类变体没有达成共识,因此所有现有方法要么是特定于平台的(例如gettext / po),要么是有局限性的(例如,一个字符串中不能有多个复数),或者不是特别是翻译器-友好,例如Java的MessageFormat / ...
一、国际化的相关知识 1、什么是国际化。 程序只有一套,能根据来访者的阅读...5、批量国际化MessageFormat 占位符:aaaa{索引,类型,样式} 索引:0... 类型:date、time、number等 样式: 6、JSTL中的fmt标签
该项目为开发人员提供了有助于使用Crowdin本地化管理平台的工具。 目前,它仅提供java.text.MessageFormat的替代方案,该替代方案不使用单引号作为转义字符。 对于Maven用户:Groupid:com.fathzer工件:jlocal
本文主要介绍javaWEB国际化的知识,这里整理了详细的资料及实现代码,有兴趣的小伙伴可以参考下
,这: Servlet 规范 3.0 JSP 规范 2.2 EL 规范 2.2 Java 6 及更高版本Java 1.7.0_67。 H2 的 。 Hibernate 4.3 需要 Jandex。 反过来,Weld 检测到 Jandex 在类路径上可用并使用。 Weld 中与 Jandex 相关的类然后...
通常来说Android手机没有未接来电的监听器,如果要实现对未接来电的处理,则需要自己编写...import java.text.MessageFormat; import rbase.app.smshelpmate.Config; import rbase.app.smshelpmate.R; import rbase.ap
2. 使用java.text.MessageFormat中的字符串格式,格式为{ 参数序号(从0开始), 格式类形(number | date | time | choice), 格式样式},例如: validation.between=Date must between {0, date, short} and {1, ...
import java.text.MessageFormat; import java.util.MissingResourceException; import org.eclipse.swt.SWT; import org.eclipse.swt.SWTError; import org.eclipse.swt.browser.Browser; import org.eclipse.swt....
Gets a message based on a key using the supplied args, as defined in MessageFormat, or, if the message is not found, a supplied default value is returned. String getText(String key, String default...
ObjectFormat - powerful MessageFormat replacement makes localization easier Command Line Parsing - capture, parse, convert and type check command line arguments Environment Variables - capture and ...
1. 介绍.........................................................................................................18 1.1. 关于本书..........................................................................