MessageFormat setFormats Example

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


MessageFormat class setFormats example. public void setFormats(Format[] newFormats) Sets the formats to use for the format elements in the previously set pattern string. The order of formats in newFormats corresponds to the order of format elements in the pattern string.

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

import java.text.*;

public class SetFormats {

    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(5)new Double(7.53),
            new java.util.Date("04-Jul-2008")
        };

        // Create a MessageFormat object and apply the pattern
        // to it.
        MessageFormat mFmt = new MessageFormat(pattern);
        mFmt.setFormats(formats);

        System.out.println(mFmt.format(pattern, values));
    }
}

Output
I bought 5 apples for Rs.7.53 on 04-Jul-2008.

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

HOME | COPYRIGHT | CONTACT US