Codementor Events

Better string formatting in Java with MessageFormat

Published Apr 12, 2024Last updated Apr 16, 2024
Better string formatting in Java with MessageFormat

This is one of the many blog posts I have. See the full list on Igor's Techno Club

When it comes to string formatting in Java, developers typically opt for either straightforward string concatenation or the String.format method. I usually recommend using String.format, because it's less error prone than string concatenation, but today I want to explore another option that is often overlooked by developers.

The main difference between MessageFormat.format and String.format lies in how arguments are passed to the string: String.format utilizes specific placement markers (e.g., %s, %d), whereas MessageFormat.format uses indexed placeholders, with each index corresponding to a value from the argument array.

String message = "Hello, {0}! Welcome to {1}.";
String formattedMessage = MessageFormat.format(message, "John", "our website");
System.out.println(formattedMessage);

Output:

Hello, John! Welcome to our website.

But there are more differences to consider. Let's delve deeper into what MessageFormat.format offers:

Handling Pluralization:

String pattern = "There {0,choice,0#are no files|1#is one file|1\<are {0,number,integer} files} on your desktop.";
for (int count : new int[]{0, 1, 5}) {
 System.out.println(MessageFormat.format(pattern, count));
}

Output:

There are no files on your desktop.
There is one file on your desktop.
There are 5 files on your desktop.

Here, MessageFormat utilizes the choice format to select the appropriate message based on the number.

Localization and Internationalization:

double payment = 1234.56;
Object[] args = {payment}; MessageFormat usFormatted = new MessageFormat("The payment due is {0,number,currency}.", Locale.US);
MessageFormat itFormatted = new MessageFormat("The payment due is {0,number,currency}.", Locale.ITALY); System.out.println(usFormatted.format(args));
System.out.println(itFormatted.format(args));

Output:

The payment due is $1,234.56.
The payment due is € 1.234,56.

MessageFormat.format supports localization by allowing the specification of a Locale object. In this example, the payment amount is formatted differently based on the locale.

String message = "The meeting is scheduled for {0, date, long} at {0, time, short}.";
Date meetingDate = new Date();
String formattedMessage = MessageFormat.format(message, meetingDate);
System.out.println(formattedMessage);

Output:

The meeting is scheduled for May 18, 2023 at 10:30 AM.

MessageFormat.format can also handle date and time formatting based on specified format styles. In this example, the meeting date is formatted using the long date style and short time style. This method also allows for more sophisticated date formatting, such as:

Date now = new Date();
String formattedFilename = MessageFormat.format("file{0,date,yyyy-MM-dd-HH-mm-ss}.json", now);
System.out.println(formattedFilename);

This exploration of MessageFormat.format demonstrates its flexibility and power, particularly in applications requiring localization, conditional formatting, and the management of complex string patterns.

Originally posted on Igorstechnoclub.com

Discover and read more posts from Igor Novokshonov
get started