Aug 15, 2012

Formatting String Messages RunTime

We can format(insert/update) the string messages using MessageFormat class in java.text package from
java.MessageFormat provides a means to produce concatenated messages in language-neutral way. Use this to construct messages displayed for end users.

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

Note: MessageFormat differs from the other Format classes in that you create a MessageFormat object with one of its constructors (not with a getInstance style factory method). The factory methods aren't necessary because MessageFormat itself doesn't implement locale specific behavior. Any locale specific behavior is defined by the pattern that you provide as well as the subformats used for inserted arguments.


Code Example:
package com.java67.utility;

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

public class MessageFormating {
 public static void main(String[] args) {

  // Scenario 1
  String message = "Hi this is {0}, How are you {1}";
  MessageFormat format = new MessageFormat(message);
  String printingMessage = format.format(new Object[] { "java67", "World!!" });
  System.err.println(printingMessage);

  // Scenario 2 using multiple types
  Object[] arguments = { new Integer(7),new Date(System.currentTimeMillis()),"a disturbance in the Force" };
  String result = MessageFormat.format("At {1,time} on {1,date,FULL}, there was {2} on planet {0,number,integer}.",arguments);
  System.out.println(result);
  
  // For more sophisticated patterns, you can use a ChoiceFormat to get output such as:
  MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0}.");
  double[] filelimits = { 0, 1, 2 };
  String[] filepart = { "no files", "one file", "{0,number} files" };
  ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
  form.setFormatByArgumentIndex(0, fileform);
  Object[] testArgs = { new Long(1111), "MyDisk" };
  
  System.out.println(form.format(testArgs));

  // Second approach
  Object[] testArgs2 = { new Long(4), "MyDisk" };
  form.applyPattern("There {0,choice,0#are no files|1#is one file|1<are files}");
  System.out.println(form.format(testArgs2));
 }
}


Format Type Format Style Subformat Created
(none) (none) null
number (none) NumberFormat.getInstance(getLocale())
integer NumberFormat.getIntegerInstance(getLocale())
currency NumberFormat.getCurrencyInstance(getLocale())
percent NumberFormat.getPercentInstance(getLocale())
SubformatPattern new DecimalFormat(subformatPattern, new DecimalFormatSymbols(getLocale()))
date (none) DateFormat.getDateInstance(DateFormat.DEFAULT, getLocale())
short DateFormat.getDateInstance(DateFormat.SHORT, getLocale())
medium DateFormat.getDateInstance(DateFormat.DEFAULT, getLocale())
long DateFormat.getDateInstance(DateFormat.LONG, getLocale())
full DateFormat.getDateInstance(DateFormat.FULL, getLocale())
SubformatPattern new SimpleDateFormat(subformatPattern, getLocale())
time (none) DateFormat.getTimeInstance(DateFormat.DEFAULT, getLocale())
short DateFormat.getTimeInstance(DateFormat.SHORT, getLocale())
medium DateFormat.getTimeInstance(DateFormat.DEFAULT, getLocale())
long DateFormat.getTimeInstance(DateFormat.LONG, getLocale())
full DateFormat.getTimeInstance(DateFormat.FULL, getLocale())
SubformatPattern new SimpleDateFormat(subformatPattern, getLocale())
choice SubformatPattern new ChoiceFormat(subformatPattern)

For more Info : click here

No comments:

Post a Comment

Main Differences Between SVN and Git