MessageFormat类解析

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(子格式模式,形如#.##)

构造函数

  • 默认语言环境
public MessageFormat(String pattern)
  • 指定了语言环境
 public MessageFormat(String pattern, Locale locale)

格式化的静态方法

 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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容