Home

MessageFormat

MessageFormat is a class in the Java standard library (java.text) that formats localized messages by combining a pattern with arguments. It enables locale-aware substitution and formatting of numbers, dates, times, and selections (pluralization) within a single string.

Pattern syntax: A pattern consists of literals and argument expressions enclosed in curly braces. An expression

Usage: Create a MessageFormat with a pattern and a Locale, then call format(Object[]). Patterns can be stored

Escaping: Literal text is quoted with single quotes. To include a literal single quote, use two consecutive

See also: ResourceBundle, ICU4J MessageFormat, java.text.MessageFormat in the Java API.

has
the
form
{index[,type[,style]]}.
index
selects
the
position
in
the
supplied
arguments
array.
type
can
be
number,
date,
time,
or
choice;
style
specifies
a
locale-specific
format
(for
example
date
or
number
formats).
If
the
type
is
omitted,
the
argument
is
formatted
using
its
toString
value.
Examples:
"Hello
{0}";
"On
{1,date,short}
at
{2,time,short}."
The
choice
form
enables
basic
pluralization
rules.
in
a
ResourceBundle
to
provide
locale-specific
strings.
Example:
MessageFormat
fmt
=
new
MessageFormat("Hello
{0},
today
is
{1,date,full}.",
Locale.US);
String
s
=
fmt.format(new
Object[]{"Alice",
new
Date()});
quotes.
To
include
a
literal
{
or
}
in
the
text,
enclose
it
within
quotes.