MessageFormat formatToCharacterIterator Example

MessageFormat formatToCharacterIterator Example


MessageFormat class formatToCharacterIterator example. public AttributedCharacterIterator formatToCharacterIterator(Object ) Formats an array of objects and inserts them into the MessageFormat's pattern, producing an AttributedCharacterIterator. You can use the returned AttributedCharacterIterator to build the resulting String, as well as to determine information about the resulting String.

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

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

public class FormatToCharacterIterator {

    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