Code example for MessageFormat

Methods: format

0
        Locale.setDefault(Locale.CANADA);
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
        String pat = "text here {0,date,yyyyyyyyy} and here";
        Calendar c = Calendar.getInstance();
        String etalon = "text here 00000" + c.get(Calendar.YEAR) + " and here";
        MessageFormat obj = new MessageFormat(pat);
        assertEquals(etalon, obj.format(new Object[] { new Date() }));
    } 
 
    public void test_parseLjava_lang_StringLjava_text_ParsePosition() { 
        ParsePosition pos = new ParsePosition(2);
 
        MessageFormat mf = new MessageFormat("{0}; {0}; {0}");
        String parse = "a; b; c";
        try { 
            mf.parse(parse, null);
            fail("NullPointerException was not thrown.");
        } catch(NullPointerException npe) {
            //expected 
        }