MessageFormat applyPattern Example
MessageFormat class applyPattern example. This example shows you how to use applyPattern method.
MessageFormat class applyPattern example. public void applyPattern(String pattern) Sets the pattern used by this message format. The method parses the pattern and creates a list of subformats for the format elements contained in it. Patterns and their interpretation are specified in the class description.
Here is the code
/*
* @ # ApplyPattern.java
* A class repersenting use to ApplyPattern method
* of MessageFormat class in java.text package
* version 17 June 2008
* author Rose India
*/
import java.text.*;
import java.util.*;
public class ApplyPattern {
public static void main(String[] args) {
String pattern = "I bought {0,number,#} apple";
java.text.Format formats[] = {new DecimalFormat()};
//Format[] formats ={new DecimalFormat("#")};
Object[] values = {new Integer(500)};
MessageFormat mFmt = new MessageFormat(pattern);
mFmt.setFormats(formats);
mFmt.applyPattern(pattern);
//Create clone
MessageFormat mFmt1 = (MessageFormat) mFmt.clone();
System.out.println(mFmt1.toPattern());
System.out.println(mFmt1.format(values));
}
} |
Output
I bought {0,number,#} apple
I bought 500 apple |
|