使用MessageFormat格式化数字,日期
http://www.cppblog.com/biao/archive/2010/12/01/135119.html
如數字上加逗號,保留小數點后面兩位(會自動四舍五入),百分比,貨幣等。
參考實例:?http://www.java2s.com/Code/Java/Development-Class/MessageFormat.htm
import java.text.MessageFormat;
public class Test {
? ? public static void main(String[] args) {
? ? ? ? Object[] params = { new Integer(123), new Double(1222234.567) };
? ? ? ? String msg = MessageFormat.format("{0,number,percent} and {1,number,,###.##}", params);
? ? ? ? System.out.println(msg);
? ? }
}
=========
http://sunxboy.iteye.com/blog/273950
第一個例子使用靜態的方法 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.?
?
=============================================================
?
Java代碼 ??
在 String 中,"''" 表示單引號。QuotedString 可以包含除單引號之外的任意字符;圍繞的單引號被移除。UnquotedString 可以包含除單引號和左花括號之外的任意字符。因此,格式化后消息字符串為 "'{0}'" 的字符串可以寫作 "'''{'0}''" 或 "'''{0}'''"。
在 SubformatPattern 中,應用了不同的規則。QuotedPattern 可包含除單引號之外的任意字符,但不移除圍繞的單引號,因此它們可以由子格式解釋。例如,"{1,number,$'#',##}" 將產生一個帶井號的數字格式,結果如:"$#31,45"。 UnquotedPattern 可以包含除單引號之外的任意字符,但其中的花括號必須成對出現。例如,"ab {0} de" 和 "ab '}' de" 是有效的子格式模式,而 "ab {0'}' de" 和 "ab } de" 則是無效的。
http://terryjs.iteye.com/blog/768872
==================
一。
MessageFormat 提供了以與語言無關方式生成連接消息的方式。使用此方法構造向終端用戶顯示的消息。
MessageFormat 獲取一組對象,格式化這些對象,然后將格式化后的字符串插入到模式中的適當位置。
注: MessageFormat 不同于其他 Format 類,因為 MessageFormat 對象是用其構造方法之一創建的(而不是使用 getInstance MessageFormat 本身不實現特定于語言環境的行為。特定于語言環境的行為是由所提供的模式和用于已插入參數的子格式來定義的。
?
?
MessageFormat運行開發者輸出文本中的變量的格式。它是一個強大的類,就像下面的例子展示的那樣:
Java代碼 String?message?= ???
隱藏在信息中的是描述輸出的格式的一種短小的代碼,范例的輸出如下:
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對象并給出信息。下面是上面的例子的修正版:
?
除了可以處理日期、時間、數字和百分數外,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 :
Java代碼 int?planet?=?7; ???
輸出為:
At 12:30 PM on Jul 3, 2053, there was a disturbance in the Force on planet 7.下面的例子創建了一個可以重復使用的 MessageFormat 實例:
Java代碼 int?fileCount?=?1273; ???
不同 fileCount 值的輸出:
The disk "MyDisk" contains 0 file(s).The disk "MyDisk" contains 1 file(s).The disk "MyDisk" contains 1,273 file(s).對于更復雜的模式,可以使用 ChoiceFormat 來生成正確的單數和復數形式:
Java代碼 MessageFormat?form?=?new?MessageFormat("The?disk?\"{1}\"?contains?{0}."); ???
不同的 fileCount 值的輸出:
The disk "MyDisk" contains no files.The disk "MyDisk" contains one file.The disk "MyDisk" contains 1,273 files.如上例所示,可以以編程方式來創建 ChoiceFormat ,或使用模式創建。有關更多信息,請參閱 ChoiceFormat 。
Java代碼 form.applyPattern( ???
注: 從上面的例子可以看到,由 MessageFormat 中的 ChoiceFormat 所生成的字符串要進行特殊處理;'{' 的出現用來指示子格式,并導致遞歸。如果 MessageFormat 和 ChoiceFormat 都是以編程方式創建的(而不是使用字符串模式),那么要注意不要生成對其自身進行遞歸的格式,這將導致無限循環。
當一個參數在字符串中被多次解析時,最后的匹配將是解析的最終結果。例如,
Java代碼 MessageFormat?mf?=?new?MessageFormat("{0,number,#.##},?{0,number,#.#}"); ???
同樣,使用包含同一參數多個匹配項的模式對 MessageFormat 對象進行解析時將返回最后的匹配。例如,
Java代碼 MessageFormat?mf?=?new?MessageFormat("{0},?{0},?{0}"); ???
?
同步
消息格式不是同步的。建議為每個線程創建獨立的格式實例。如果多個線程同時訪問一個格式,則它必須是外部同步的。
?
總結
以上是生活随笔為你收集整理的使用MessageFormat格式化数字,日期的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: subprogram or cursor
- 下一篇: Hibernate 批量插入、更新与删除