MessageFormat(动态文本)

本文介绍了 Java 中 MessageFormat 类的基本用法及其实现国际化的技巧。MessageFormat 类支持使用占位符来格式化字符串中的日期、数字等内容,并可以根据不同的区域设置进行国际化显示。文章通过实例演示了如何使用 MessageFormat 类来实现包含日期、数字等占位符的字符串格式化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 MessageFormat:如果一段字符串中有多个对地区敏感的信息,可以使用此类进行批量国际化设置
                    (1)MessageFormat类允许开发人员用占位符{0}{1}{2}…替换掉字符串中的敏感数据(即国际化相关的数据)。
                    (2)MessageFormat类在格式化输出包含占位符的文本时,messageFormat类可以接收一个参数数组,以替换文本中的每一个占位符。
                    (3)占位符有三种方式书写方式:
                                        {argumentIndex}: 0-9 之间的数字,表示要格式化对象数据在参数数组中的索引号
                                        {argumentIndex,formatType}: 参数的格式化类型
                                        {argumentIndex,formatType,FormatStyle}: 格式化的样式,它的值必须是与格式化类型相匹配的合法模式、或表示合法模式的字符串。
                                       
                                        formatType:
                                             number
                                             date
                                             time
                                             choice
                                        fomatStyle
                                             short
                                             medium
                                             long
                                             full
                                             integer
                                             currency
                                             percent
                                             subformatpattern
                    (4)例子
                                   //模式字符串
                                   //At 12:30 pm on jul 3,1998, a hurricance destroyed 99 houses and caused $1000000 of damage
                                   @Test
                                   public void demo6(){
                                        String str = "On {0}, a hurricance destroyed {1} houses and caused {2} of damage.";
                                        Calendar calendar = Calendar.getInstance();
                                        calendar.set(1998, 6, 3, 12, 30, 0);
                                        Date date = calendar.getTime();
                                        DateFormat format = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT, Locale.CHINA);
                                        String dateStr = format.format(date);
                                        Object [] objs = {dateStr,99,"$1000000"};
                                        String result = MessageFormat.format(str,objs);
                                        System.out.println(result);
                                   }
                                  
                                        @Test
                                        public void test(){
                                             //At 12:30 pm on jul 3,1998, a hurricance destroyed 99 houses and caused $1000000 of damage
                                             String msg = "At {0,date,FULL}, a hurricance destroyed {1,number} houses and caused {2,number,currency} of damage";
                                             MessageFormat format = new MessageFormat(msg,Locale.CHINA);
                                            
                                             Calendar  c = Calendar.getInstance();
                                             c.set(1998, 6, 3);
                                             Date date = c.getTime();
                                            
                                             Object [] objs = {date,99,1000000};
                                             String str = format.format(objs);
                                             System.out.println(str);
                                            
                                        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值