Monday 22 February 2016

How to format messages in Java?

MessageFormat

The MessageFormat class can be used quite nicely to compose messages.

MessageFormat takes a set of objects, formats them, then inserts the formatted strings into the pattern at the appropriate places.

import java.text.*;

public class MessageFormator {

     public static void main(String[] args) {
           String message="Request id# {0} will be resolve till {1}.";
           Object values[] = { "1325", "25-Mar-2016" };
           String s = MessageFormat.format(message, values);
           System.out.println(s);
    }
}
Output:
Request id# 1325 will be resolve till 25-Mar-2016.







No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...