This example will show how to format a message with java's MessageFormat. MessageFormat provides a way to concatenate messages by accepting a set of objects, formats them, and then inserts the strings into the pattern specified. As noted in the java docs the MessageFormat class doesn't handle locale specifics so if it is required you must provide it with the pattern and sub formats.
Without MessageFormat you are handling the message, formats and variables to be inserted which is a form of string concatenation.
In this snippet we will call MessageFormat.format which accepts a format and an array of arguments. Both arguments in the object array are pre formatted, first is a number with a comma and second is a date in MM/dd/yyyy format.
MessageFormat multiple records
To format multiple messages we will first create a multidimensional array containing two objects to be injected into the message. Next we will define a MessageFormat with the necessary placeholders and then loop over statementValues. The formatter.format produces a String which we will collect into an ArrayList. This is different than the snippet above as we instantiate the MessageFormat object to be called multiple times.
MessageFormat format & style
In both of the snippets above the values were pre formatted. This will show how you can define the MessageFormat and a pattern to be used to format the given arguments. The first argument will format the number as currency while the second will format the java.util.Date object in MM/dd/yyyy format. A side note, we are using Joda Time to create statementDate.
MessageFormat w/ choice
This snippet will show how to provide flexibility when the values of the objects are not present. Breaking down the pattern, the first parameter will format the date in MM/dd/yyyy format and the second will have a choice. If the value is specified in the given arguments then a it will be formatted as currency, if a values isn't present a predefined message will be presented.