MessageFormat setFormatsByArgumentIndex Example

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


MessageFormat class setFormatsByArgumentIndex example.public void setFormatByArgumentIndex(int argumentIndex, Format newFormat) Sets the format to use for the format elements within the previously set pattern string that use the given argument index. The argument index is part of the format element definition and represents an index into the arguments array passed to the format methods or the result array returned by the parse methods.

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

import java.text.*;

public class SetFormatByArgumentIndex {

    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.setFormatByArgumentIndex(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.

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

HOME | COPYRIGHT | CONTACT US