Format接口的一個實現類:實現了將數據集合對象進行格式化插入到特定的模式字符串中輸出到用戶。
版本
1.7
Format其實實現類型的差異
沒有特定的模式,根據用戶傳入特定的參數進行匹配的格式化類型進行格式話
線程安全
不安全
pattern格式
格式 :{FormatElement,FormatType,FormatStyle}
demo:
{0,number,#.##}
FormatElement
指定索引的位置
FormatType
值范圍
- number 調用NumberFormat進行格式化
- date 調用DateFormat進行格式化
- time 調用DateFormat進行格式化
- choice 調用ChoiceFormat進行格式化
FormatStyle
值范圍如下:
short,medium,long,full,integer,currency,percent,SubformPattern(子格式模式,形如#.##)
構造函數
- 默認語言環(huán)境
public MessageFormat(String pattern)
- 指定了語言環(huán)境
public MessageFormat(String pattern, Locale locale)
格式化的靜態(tài)方法
public static String format(String pattern, Object ... arguments) {
MessageFormat temp = new MessageFormat(pattern);
return temp.format(arguments);
}
demo
System.out.println(MessageFormat.format("{1,number,currency}", 100000.1,111));
System.out.println(MessageFormat.format("{0,number,percent}", 0.12));
System.out.println(MessageFormat.format("{0,date,short}", new Date()));
System.out.println(MessageFormat.format("{0,date,yyyy-MM-dd}", new Date()));
結果
CNY111.00
12%
9/12/18
2018-09-12
和fomat實現類之間的關系

messageFomat跟format的實現類的關系圖.png