MessageFormat setFormat Example
MessageFormat class setFormat example. This example shows you how to use setFormat method.
MessageFormat class setFormat example. public void setFormat(int formatElementIndex, Format newFormat) Sets the format to use for the format element with the given format element index within the previously set pattern string. The format element index is the zero-based number of the format element counting from the start of the pattern string.
Here is the code
/*
* @ # SetFormat.java
* A class repersenting use to setFormat method
* of MessageFormat class in java.text package
* version 19 June 2008
* author Rose India
*/
import java.text.*;
public class SetFormat {
public static void main(String[] args) {
// Create a pattern for our MessageFormat object to use.
String pattern = "I bought {0,number,#} " +
"apples for {1,number,currency} " +
"on {2,date,dd-MMM-yyyy}.";
java.text.Format[] formats = {new DecimalFormat("#"),
NumberFormat.getCurrencyInstance(),
new SimpleDateFormat("MMM/dd/yyyy")
};
Object[] values = {new Integer(5), new Double(7.53),
new java.util.Date("04-Jul-2004")
};
MessageFormat mFmt = new MessageFormat(pattern);
mFmt.setFormat(0, mFmt);
mFmt.applyPattern(pattern);
System.out.println(mFmt.format(pattern, values));
}
} |
Output
| I bought 5 apples for Rs.7.53 on 04-Jul-2004. |
|