MessageFormat getFormats Example

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


MessageFormat class getFormats example. public Format[] getFormats() Gets the formats used for the format elements in the previously set pattern string. The order of formats in the returned array corresponds to the order of format elements in the pattern string.

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

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

public class getFormats {

    public static void main(String args[]) {

        String pattern = "We are {0,number,#} boys and {1,number,#} girls";
        DecimalFormat formats[] {new DecimalFormat()};
        //Format[] formats ={new DecimalFormat("#")};
        Object[] values = {new Integer(5)new Integer(10)};
        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[] f = mFmt.getFormats();
        for (int i = 0; i < f.length; i++) {
            System.out.println(f[i].getClass());
        }
    }
}

Output
We are {0,number,#} boys and {1,number,#} girls
We are 5 boys and 10 girls
class java.text.DecimalFormat
class java.text.DecimalFormat

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

HOME | COPYRIGHT | CONTACT US