Overview
In this article, we will take a look at MessageFormat class is, its use with examples and explanation.
After reading this article, you should have a good understanding of how to use this powerful class to format messages in your Java applications.

Java MessageFormat

java.text.MessageFormat class is a powerful tool for formatting messages in Java. It provides a convenient way to create and format messages dynamically.
This means that you can define placeholders in a string and replace these placeholders with values from variables.
Thus, you can create strings at runtime without concatenation.

Java docs for MessageFormat states,

MessageFormat provides a means to produce concatenated messages in a language-neutral way. Use this to construct messages displayed for end users.

MessageFormat works by taking a pattern string and arguments, and then substituting the arguments into the pattern string in their appropriate positions.
The pattern string is a string that contains placeholders for the arguments.
The placeholders are numbered and surrounded with curly braces, and the arguments are substituted into the placeholders in the order in which they appear in the argument list.
Syntax
To use MessageFormat, you need to create its object using a constructor.
Its constructor takes a string that contains placeholders or the pattern string as

public MessageFormat(String pattern)

For example, the pattern string “There are {0} apples and {1} oranges.” could be formatted using an array containing two objects, “10” and “20”.
The result would be the string “There are 10 apples and 20 oranges.”

MessageFormat example
To replace placeholders with values, format() method of MessageFormat is used.
You can call format() either using an object of MessageFormat or using static format() method. We will look at both these methods.

int fileCount = 2345;
String diskName = "MyDisk";
Object[] testArgs = {new Long(fileCount), diskName};
MessageFormat m = new MessageFormat(
            "The disk \"{1}\" contains {0} file(s).");
String message = m.format(testArgs);
System.out.print(message);

Output is

The disk “MyDisk” contains 2345 files.

Notice how it substituted the placeholders with array elements at corresponding index.

Another way is by using the static format() method as shown below

Object[] testArgs = {new Long(2345), "MyDisk"}; 
String message = MessageFormat.format(
                     "The disk \"{1}\" contains {0} file(s).",
                     testArgs);
System.out.print(message);

Note that the static format() accepts the string or pattern and object array as arguments.

Advanced Formatting
MessageFormat supports advanced formatting options where you can provide formatting style along with placeholders.
So, you can instruct MessageFormat to format an argument as a date, currency, time etc.
These options are provided within curly braces after index. Example,

String message = "At {0,time, long} on {0,date}," +
                 " {1} paid {2,number, currency} for {3}.";
Object[] arguments = {
  new java.util.Date(),
  "A B",
  4.259,
  "a piece of gum"
};
String result = MessageFormat.format(message, arguments);
System.out.println(result);
message = "At {0,time, full} on {0,date, medium}," +
          " {1} paid {2,number, currency} for {3}.";
result = MessageFormat.format(message, arguments);
System.out.println(result);
message = "At {0,time, medium} on {0,date}," +
          " {1} paid {2,number} for {3}.";
result = MessageFormat.format(message, arguments);
System.out.println(result);

Output is

At 1:29:18 pm IST on 17-Dec-2022, A B paid ₹4.26 for a piece of gum.
At 1:29:18 pm India Standard Time on 17-Dec-2022, A B paid ₹4.26 for a piece of gum.
At 1:29:18 pm on 17-Dec-2022, A B paid 4.259 for a piece of gum.

Note that the second argument is of type java.util.Date().
Notice different ways in which date and time are printed. These values are extracted from the supplied date object.

Following is a table of options with each format type

FormatStyle
numberinteger
currency
percent
dateshort
medium
long
full
timeshort
medium
long
full

Remember that when nothing is supplied for style, it uses the default formats returned by
NumberFormat.getInstance(),
DateFormat.getDateInstance() and
DateFormat.getTimeInstance()
methods for number, date and time formats respectively.

You can try each of style values and see the output yourself.

That is all on formatting a string with MessageFormat class. Hope it was useful.