MessageFormat toPattern Example

MessageFormat class toPattern example. This example shows you how to use toPattern method.


MessageFormat class toPattern example. public String toPattern() Returns a pattern representing the current state of the message format. The string is constructed from internal information and therefore does not necessarily equal the previously applied pattern.

Here is the code
/*
 * @ # ToPattern.java
 * A class repersenting use to setLocale method 
 * of MessageFormat class in java.text package
 * version 19 June 2008
 * author Rose India 
 */

import java.text.*;

public class ToPattern {

    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}.";

        // Specify which formatters are to be used for each
        // of the placeholders in the pattern above.
        java.text.Format[] formats = {new DecimalFormat("#"),
            NumberFormat.getCurrencyInstance(),
            new SimpleDateFormat("MMM/dd/yyyy")
        };

        // Create values to populate the position in the pattern.
        Object[] values = {new Integer(10)new Double(17.53),
            new java.util.Date("14-Jul-2008")
        };

        // Create a MessageFormat object and apply the pattern
        // to it.
        MessageFormat mFmt = new MessageFormat(pattern);
        mFmt.setFormatsByArgumentIndex(formats);
        mFmt.setLocale(new java.util.Locale("English""India"));

        // Print out the pattern being used for formatting
        // and the formatted output.
        System.out.println(mFmt.toPattern());
        System.out.println(mFmt.format(pattern, values));
    }
}

Output
I bought {0,number,#} apples for {1,number,¤#,##0.00} on {2,date,MMM/dd/yyyy}.
I bought 10 apples for Rs.17.53 on 14-Jul-2008.

Post Comment
Name:
E-mail:
Contact no :
Comments:
  Refresh Image
Verify Image:
 
 
Your Comment's
 
 
 

HOME | COPYRIGHT | CONTACT US