MessageFormat getFormatsByArgumentIndex Example

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


MessageFormat class getFormatsByArgumentIndex example. public Format[] getFormatsByArgumentIndex() Gets the formats used for the values passed into format methods or returned from parse methods. The indices of elements in the returned array correspond to the argument indices used in the previously set pattern string.

Here is the code
/*
 * @ # GetFormatsByArgumentIndex.java
 * A class representing use to GetFormatsByArgumentIndex
 * method
of MessageFormat class in java.text package
 * version 17 June 2008
 * author Rose India 
 */

import java.text.*;
import java.util.Date;

public class GetFormatsByArgumentIndex {

    public static void main(String[] args) {

        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 Date("04-Jul-2004")
        };

        MessageFormat mFmt = new MessageFormat(pattern);

        mFmt.setFormats(formats);
        mFmt.applyPattern(pattern);

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

        java.text.Format format[] = mFmt.getFormatsByArgumentIndex();
        for (int i = 0; i < format.length; i++) {
            System.out.println(format[i]);
        }
    }
}

Output
I bought {0,number,#} apples for {1,number,currency} on {2,date,dd-MMM-yyyy}.
I bought 5 apples for Rs.7.53 on 04-Jul-2004.
java.text.DecimalFormat@674dc
java.text.DecimalFormat@7b6c9
java.text.SimpleDateFormat@541ff80d

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

HOME | COPYRIGHT | CONTACT US