Monday 1 June 2015

Java MessageFormat class

MessageFormat class is used to separate message structure from message content. It is always good to explain, with a simple example.

Step 1: Define message structure (I call it as a pattern).
String pattern = "I am {0}. I am {1} years old. I am living in {2}";

Step 2:  Define some data to populate pattern while formatting.
String emp1[] = { "Hari Krishna Gurram", "26", "Bangalore" };

Step3: Define MessageFormat object with the pattern defined in step 1.
MessageFormat messageFormat = new MessageFormat(pattern);

Step 4: Format the string with given data.
messageFormat.format(emp1, result1, null);

After formatting result is stored in result1.

result1 contains data “I am Hari Krishna Gurram. I am 26 years old. I am living in Bangalore”.

{0} Is replaced with emp1[0].
{1} Is replaced with emp1[1].
{2} Is replaced with emp1[2].

Complete example given below.
import java.text.MessageFormat;

public class MessageFormatEx {
 public static void main(String args[]) {
  /* Define a pattern */
  String pattern = "I am {0}. I am {1} years old. I am living in {2}";

  /* Define data to populate the pattern while formatting */
  String emp1[] = { "Hari Krishna Gurram", "26", "Bangalore" };
  String emp2[] = { "Sankalp Dubey", "31", "Bangalore" };
  String emp3[] = { "Sailaja PTR", "27", "Bangalore" };

  /* StringBuffer objects to store final result */
  StringBuffer result1 = new StringBuffer();
  StringBuffer result2 = new StringBuffer();
  StringBuffer result3 = new StringBuffer();

  /* MessageFormat object for given pattern */
  MessageFormat messageFormat = new MessageFormat(pattern);

  messageFormat.format(emp1, result1, null);
  System.out.println(result1);

  messageFormat.format(emp2, result2, null);
  System.out.println(result2);

  messageFormat.format(emp3, result3, null);
  System.out.println(result3);

 }
}


Output
I am Hari Krishna Gurram. I am 26 years old. I am living in Bangalore
I am Sankalp Dubey. I am 31 years old. I am living in Bangalore
I am Sailaja PTR. I am 27 years old. I am living in Bangalore

MessageFormat uses patterns of the following form:
 MessageFormatPattern:
         String
         MessageFormatPattern FormatElement String

 FormatElement:
         { ArgumentIndex }
         { ArgumentIndex , FormatType }
         { ArgumentIndex , FormatType , FormatStyle }

 FormatType: one of number, date, time, choice

 FormatStyle:
         short
         medium
         long
         full
         integer
         currency
         percent

         SubformatPattern

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

public class MessageFormatEx {
 public static void main(String args[]) {
  /* Define a pattern */
  String pattern = "logged at time {0,time} on date {0, date}";

  /* StringBuffer objects to store final result */
  StringBuffer result1 = new StringBuffer();

  /* MessageFormat object for given pattern */
  MessageFormat messageFormat = new MessageFormat(pattern);

  messageFormat.format(new Object[] { new Date() }, result1, null);

  System.out.println(result1);
 }
}


Output

logged at time 7:12:30 PM on date 1 Jun, 2015



No comments:

Post a Comment