MessageFormatNameMessageFormatSynopsis
DescriptionThe MessageFormat class constructs textual messages using a formatting pattern string. Conceptually, the class functions much like printf() in C. Syntactically, however, it is quite different. A MessageFormat object uses a pattern string; formatted arguments are placed into the pattern string to produce a resulting string. Arguments are delimited by matching sets of curly braces and may include additional information about how that data should be formatted. For example, consider the following code:
String message = "Boot of server {0}began at {1, time}on {1, date, full}."; MessageFormat boot = new MessageFormat(message); Date now = new Date(); Object[] arguments = {"luna3", now}; System.out.println(boot.format(arguments)); This code produces the following output:
Boot of server luna3 began at 11:13:22 AM on Monday, March 03, 1997. Each of the arguments is numbered and includes an optional type and an optional style. In the example above, {1, date, full} indicates that the argument at index 1 in the argument array should be formatted using a DateFormat object with the FULL style. The allowed types and styles are:
For the date and time types, the styles correspond to the styles, or lengths, of the resulting date and time strings. You can also specify a date or time pattern string as you would for creating a SimpleDateFormat object. For the number type, the styles correspond to formatting normal numbers, percentage values, and currency values. You can also specify a number pattern string as you would for creating a DecimalFormat object. For the choice type, you can specify a choice pattern as you would for creating a ChoiceFormat object. If no type is specified, the argument should be a string. The following example shows how to use a choice format pattern with a MessageFormat:
Object[] arguments = {new Integer(1)}; String grammar = "At last count, {0}server{0, choice, 0#s|1#|1<s} {0, choice, 0#were|1#was|1<were}booted."; MessageFormat booted = new MessageFormat(grammar); System.out.println(booted.format(arguments)); arguments[0] = new Integer(2); System.out.println(booted.format(arguments)); This example produces the following output:
At last count, 1 server was booted. At last count, 2 servers were booted. As an alternative to specifying all of the formatting in the pattern string, you can use an array of Format objects to format the arguments. You can specify this array using setFormats(). Note that you create MessageFormat objects directly, rather than through factory methods. This is because MessageFormat does not implement any locale-specific behavior. To produce properly internationalized output, the pattern string that is used to construct a MessageFormat should come from a ResourceBundle instead of being embedded in the code. Class Summary
public class java.text.MessageFormat extends java.text.Format { // Constructors public MessageFormat(String pattern); // Class Methods public static String format(String pattern, Object[] arguments); // Instance Methods public void applyPattern(String newPattern); public Object clone(); public boolean equals(Object obj); public final StringBuffer format(Object source, StringBuffer result, FieldPosition ignore); public final StringBuffer format(Object[] source, StringBuffer result, FieldPosition ignore); public Format[] getFormats(); public Locale getLocale(); public int hashCode(); public Object[] parse(String source); public Object[] parse(String source, ParsePosition status); public Object parseObject(String text, ParsePosition status); public void setFormat(int variable, Format newFormat); public void setFormats(Format[] newFormats); public void setLocale(Locale theLocale); public String toPattern(); } ConstructorsMessageFormatpublic MessageFormat(String pattern)
Class Methodsformatpublic static String format(String pattern, Object[] arguments)
Instance MethodsapplyPatternpublic void applyPattern(String pattern)
clonepublic Object clone()
equalspublic boolean equals(Object obj)
format
public StringBuffer format(Object source, StringBuffer result, FieldPosition ignore)
public StringBuffer format(Object[] source, StringBuffer result, FieldPosition ignore)
getFormatspublic Format[] getFormats()
getLocalepublic Locale getLocale()
hashCodepublic int hashCode()
parsepublic Object[] parse(String source) throws ParseException
public Object[] parse(String source, ParsePosition status)
parseObjectpublic Object parseObject(String text, ParsePosition status)
setFormatpublic void setFormat(int variable, Format newFormat)
setFormatspublic void setFormats(Format[] newFormats)
setLocalepublic void setLocale(Locale theLocale)
toPatternpublic String toPattern()
Inherited Methods
See AlsoChoiceFormat, DateFormat, FieldPosition, Format, Locale, NumberFormat, ParseException, ParsePosition, ResourceBundle, String, StringBuffer | |||||||||||||||||||||||||||||||||||||||
|