Class MessageFormat

java.lang.Object
java.text.Format
java.text.MessageFormat
All Implemented Interfaces:
Serializable, Cloneable

public class MessageFormat extends Format

Builds a message from a pattern and a list of arguments, and reads the arguments back out of a message.

Pattern syntax

Text is copied as it is, and each {...} element is replaced by an argument:

  • {0}: the argument at that index. A Number is formatted with NumberFormat.getInstance, a Date with the short date and time format, a String is copied, anything else contributes toString().
  • {0,number}, {0,number,integer}, {0,number,percent}, {0,number,currency}, or {0,number,#,##0.00} with a DecimalFormat pattern.
  • {0,date} and {0,time}, optionally followed by short, medium, long or full, or by a SimpleDateFormat pattern: {0,date,yyyy-MM-dd}. The device's own java.text.DateFormat does the formatting, so the named styles produce what the device produces.
  • {0,choice,0#none|1#one|1<{0} items} with a ChoiceFormat pattern. If the chosen text has elements of its own, they are formatted against the same arguments.

A single quote makes the characters up to the next one literal, which is how a { is written ('{'), and two quotes in a row stand for one. An apostrophe in a message must therefore be doubled: it''s.

An argument that is null is written as null, and an element whose index is past the end of the arguments is written back as it was, {2}.

Differences from the JDK
  • toPattern returns the elements as they were written rather than a canonical rewrite of them.
  • Date and time elements ignore the locale passed to the constructor: the device formats dates for its own locale only.
See Also:
  • Constructor Details

    • MessageFormat

      public MessageFormat(String pattern)
    • MessageFormat

      public MessageFormat(String pattern, Locale locale)
  • Method Details

    • setLocale

      public void setLocale(Locale locale)
    • getLocale

      public Locale getLocale()
    • applyPattern

      public void applyPattern(String pattern)
    • toPattern

      public String toPattern()
    • format

      public static String format(String pattern, Object... arguments)
    • format

      public final StringBuffer format(Object arguments, StringBuffer result, FieldPosition pos)

      Formats an array of arguments, which arguments must be.

      Throws
      • IllegalArgumentException: if arguments is not an Object[], or an argument is not of the type its element formats
      Specified by:
      format in class Format
    • format

      public final StringBuffer format(Object[] arguments, StringBuffer result, FieldPosition pos)
    • getFormats

      public Format[] getFormats()
      The formats of the elements, in the order the elements appear in the pattern; null for an element that has none.
    • getFormatsByArgumentIndex

      public Format[] getFormatsByArgumentIndex()
      The formats of the arguments, indexed by argument number; null for an argument no element formats. Where several elements name the same argument, the last one's format is the one returned.
    • setFormat

      public void setFormat(int formatElementIndex, Format newFormat)

      Sets the format of the element at formatElementIndex, counted in the order the elements appear in the pattern.

      Throws
      • ArrayIndexOutOfBoundsException: if there is no such element
    • setFormats

      public void setFormats(Format[] newFormats)
      Sets the formats of the elements in pattern order; extra formats are ignored and elements past the end of newFormats are left alone.
    • setFormatByArgumentIndex

      public void setFormatByArgumentIndex(int argumentIndex, Format newFormat)
      Sets the format of every element that formats argument argumentIndex.
    • setFormatsByArgumentIndex

      public void setFormatsByArgumentIndex(Format[] newFormats)
      Sets the formats of the arguments, indexed by argument number.
    • parse

      public Object[] parse(String source, ParsePosition pos)
      Reads the arguments back out of source, starting at pos: the literal text must match exactly, an element with a format is read by that format, and an element without one takes the text up to the literal that follows it. The result is indexed by argument number. On failure pos is left alone, its error index is set, and null is returned.
    • parse

      public Object[] parse(String source) throws ParseException

      Reads the arguments back out of the beginning of source.

      Throws
      • ParseException: if source does not match the pattern
      Throws:
      ParseException
    • parseObject

      public Object parseObject(String source, ParsePosition pos)
      Description copied from class: Format
      Parses an object from source starting at pos. On success the index of pos is moved past what was read; on failure it is left alone, the error index is set and null is returned.
      Specified by:
      parseObject in class Format
    • clone

      public Object clone()
      A copy of this format, built by applying its pattern again: Object.clone() answers null on the device for anything but an array. A format installed with setFormat that the pattern cannot spell is carried over as the same object, since it cannot be copied the same way.
      Overrides:
      clone in class Object
    • equals

      public boolean equals(Object obj)
      Description copied from class: Object
      Indicates whether some other object is "equal to" this one. The equals method implements an equivalence relation: It is reflexive: for any reference value x, x.equals(x) should return true. It is symmetric: for any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. It is transitive: for any reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true. It is consistent: for any reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the object is modified. For any non-null reference value x, x.equals(null) should return false. The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Description copied from class: Object
      Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable. The general contract of hashCode is: Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables. As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
      Overrides:
      hashCode in class Object