Top 3 Java Text Formatting examples| MessageFormat class in java

java MessageFormat Text Formatting: Learn with syntax and examples.

Why MessageFormat is introduced?

the disadvantage with String class processing is Strings are immutable objects, more objects are created in heap memory and these messages order is not same for every language, because of this problems, Sun has introduced text format classes like MessageFormat

Strings are immutable objects, which means that many objects are created in heap memory, and the order of these strings varies by programming language. To address these concerns, Java has offered text format classes such as MessageFormat.

Here is an example to format a string in java

String stringText="Hi"+ name+ "How are u";

MessageFormat is a text format class in the java.text package that was introduced in the java5 language and is used to add support for Internalization.

java.util.MessageFormat class provides capabilities to display localization-specific messages and format the messages as per language-specific.

In Any application, messages are displayed to users when validation fails or a request is submitted successfully.

In real-world programs, messages are stored in resource bundles or property files, programs read the properties files based on language.

MessageFormat class simple example

Here is an example to replace parameters in a string

import java.text.MessageFormat;

public class VarargsExample {
    public static void main(String[] args) {
            Object userInformation={"John","success"};
            String messageText=" user {username} data is submitted with {status} message";
            MessageFormat messageFormatExample=new MessageFormat(messageText);
            System.out.println(messageFormatExample.format(userInformation));

        }

}

output :

user John data is submitted with the success message

In above code, format() method formats strings by receiving 0,1 arguments and MessageFormat is easy to learn and implement

How to format text messages containing Date fields?

MessageFormat also process which contains Date and currency fields, For this we have to specify the date format placeholders as below.

import java.text.MessageFormat;
import java.util.Date;
import java.util.Locale;

public class FormatDateExample {
    public static void main(String[] args) {
        Date currentDate = new Date();
        Locale.setDefault(Locale.US);
        System.out.println(MessageFormat.format("Current Date is {0,date yyyy-MM-dd}", currentDate));
    }
}

output:

The current Date is 2013-05-03

How to format text messages containing number fields?

Numbers in messages are formatted using {0, number,000.000}, which display the numbers with 3 decimals.

import java.text.MessageFormat;
import java.util.Locale;

public class VarargsExample {
    public static void main(String[] args) {
        System.out.println(MessageFormat.format("Number is  {0,number,000.000}", 123456));

    }
}

output:

Number is 123.456