JavaWeb-国际化之MessageFormat

MessageFormat:

可以格式化模式字符串

模式字符串:带占位符的字符串:"Date:{0},Salary:{1}"

可以通过format方法会模式字符串进行格式化

可以更改表达格式

    @Test
    public void testMessageFormat(){
        String str = "Date:{0},Salary:{1}";
        Locale locale = Locale.CHINA;
        Date date = new Date();
        double sal = 12345.12;
        DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM,locale);
        String dateStr = dateFormat.format(date);
        
        NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
        String salStr = numberFormat.format(sal);
        
        String result = MessageFormat.format(str,dateStr,salStr);
        System.out.println(result);
    }

  Date:2019-7-23,Salary:¥12,345.12

另外一种:

    @Test
    public void testMessageFormat(){
        String str = "Date:{0},Salary:{1}";
        Locale locale = Locale.CHINA;
        Date date = new Date();
       double sal = 12345.12;

        String result = MessageFormat.format(str,date,sal);
        System.out.println(result);
    }

  Date:19-7-23 下午3:52,Salary:12,345.12

原文地址:https://www.cnblogs.com/yangHS/p/11232281.html