MessageFormat
Home ] Up ] [ MessageFormat ] Explicit Formats ] Summary of Patterns ] Formatting Numbers ]

 

 

MessageFormat provides a means to produce concatenated messages in natural language-neutral way. You use this to construct messages displayed for end users.  MessageFormat takes a set of objects, formats them, then inserts the formatted strings into the pattern at the appropriate places.

For example:

package inputOutput;

import java.text.MessageFormat;
import java.util.GregorianCalendar;

public class FormatText
{
    public static void main(String[] args)
    {
        String pattern = 
         "On {2}, a {0} destroyed {1} houses and caused {3} of damage.";
        MessageFormat msgFormat = new MessageFormat(pattern);
        Object[] msgArgs =
        { 
            "hurricane", 
            new Integer(99), 
            new GregorianCalendar(1999, 0, 1).getTime(),
            new Double(10E7)
        };
        System.out.println(msgFormat.format(msgArgs));
    }
}

which outputs:

On 1/1/99 12:00 AM, a hurricane destroyed 99 houses and caused 100,000,000 of damage.

 
The page was last updated February 19, 2008