
Durgesh Tiwari
Author
MessageFormat in Java is a class used to create dynamic and formatted messages by inserting values into predefined text templates. It is commonly used in internationalization (i18n), localization (l10n), and multilingual applications.
In simple words:
MessageFormathelps create dynamic messages by replacing placeholders with actual values.
The MessageFormat class is part of the java.text package.
It helps developers:
Create parameterized messages
Insert dynamic values into text
Format dates and numbers
Build locale-aware applications
Support multilingual systems
MessageFormat is widely used in enterprise and international applications.
In real-world applications, messages often contain dynamic data such as:
User names
Prices
Dates
Order IDs
Error messages
Instead of writing multiple hardcoded messages, MessageFormat helps generate messages dynamically.
Reduces hardcoded text
Improves code readability
Supports localization and internationalization
Simplifies multilingual applications
Works efficiently with ResourceBundle
MessageFormat is commonly used in Spring Boot, banking systems, e-commerce platforms, and enterprise applications.
Parameterized messages are dynamic messages that use placeholders to insert values at runtime.
MessageFormat replaces these placeholders with actual data when the application executes.
Syntax
MessageFormat.format(
"Hello {0}",
"Durgesh");Output
Hello DurgeshHere:
{0} represents the first parameter
Values are inserted dynamically into the message template
MessageFormat can insert multiple dynamic values into a message using placeholders.
This helps applications generate flexible and reusable messages at runtime.
Example
import java.text.MessageFormat;
public class Test {
public static void main(String[] args) {
String message =
MessageFormat.format(
"Welcome {0} to {1}",
"Durgesh",
"Java");
System.out.println(message);
}
}Output
Welcome Durgesh to JavaHere:
{0} represents the first parameter
{1} represents the second parameter
MessageFormat automatically replaces placeholders with actual values at runtime.
MessageFormat supports multiple placeholders, allowing applications to insert several dynamic values into a single message.
import java.text.MessageFormat;
public class Test {
public static void main(String[] args) {
String message =
MessageFormat.format(
"Order {0} placed successfully for ₹{1}",
101,
5000);
System.out.println(message);
}
}Output
Order 101 placed successfully for ₹5000Here:
{0} represents the order ID
{1} represents the amount
MessageFormat automatically replaces all placeholders with actual runtime values.
MessageFormat supports locale-sensitive formatting for dates, numbers, currencies, and time values.
This helps applications display dynamic content according to regional and language settings.
import java.text.MessageFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) {
String message =
MessageFormat.format(
"Today is {0,date,long}",
new Date());
System.out.println(message);
}
}Output
Today is May 28, 2026MessageFormat automatically formats the date according to the current locale settings.
This feature is useful for international and multilingual applications.
MessageFormat can automatically format numbers and currency values according to locale settings.
This helps applications display financial and numeric data in a user-friendly format.
Example
import java.text.MessageFormat;
public class Test {
public static void main(String[] args) {
String message =
MessageFormat.format(
"Total Amount: {0,number,currency}",
5500);
System.out.println(message);
}
}Here:
number → Formats numeric values
currency → Displays the value in currency format
MessageFormat automatically formats currency values based on locale settings.
ResourceBundle and MessageFormat are commonly used together to build multilingual and dynamic applications.
ResourceBundle stores locale-specific messages, while MessageFormat inserts dynamic values into those messages at runtime.
welcome=Hello {0}, welcome to {1}import java.text.MessageFormat;
import java.util.ResourceBundle;
public class Test {
public static void main(String[] args) {
ResourceBundle rb =
ResourceBundle.getBundle("messages");
String pattern =
rb.getString("welcome");
String message =
MessageFormat.format(
pattern,
"Durgesh",
"Java");
System.out.println(message);
}
}Output
Hello Durgesh, welcome to JavaThis combination helps applications:
Support multiple languages
Generate dynamic messages
Simplify localization
Improve maintainability
It is widely used in enterprise, banking, e-commerce, and multilingual Java applications.
MessageFormat and ResourceBundle are commonly used together in multilingual applications to display messages in different languages dynamically.
Hello Rahul, your order has been shipped.Bonjour Rahul, votre commande a été expédiée.नमस्ते Rahul, आपका ऑर्डर भेज दिया गया है।ResourceBundle automatically selects the correct language-specific property file, and MessageFormat inserts dynamic values such as user names, order IDs, and prices into the message.
This approach is widely used in:
E-commerce applications
Banking systems
Travel platforms
Enterprise software
Multilingual websites
Placeholder | Meaning |
|---|---|
| Represents the first parameter |
| Represents the second parameter |
| Represents the third parameter |
Placeholders are replaced automatically with actual values during runtime.
MessageFormat in Java is a powerful class used for creating dynamic, formatted, and locale-aware messages. It helps developers build flexible, reusable, and multilingual applications efficiently.
Using MessageFormat, developers can:
Create parameterized messages
Insert dynamic values into text
Format dates, numbers, and currencies
Support localization and internationalization
Improve user experience in global applications