MessageFormat setLocale Example

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


MessageFormat class setLocale example. public void setLocale(Locale locale) Sets the locale to be used when creating or comparing subformats.

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

import java.text.*;

public class SetLocale {

    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.setFormatsByArgumentIndex(formats);
        mFmt.setLocale(new java.util.Locale("English""India"));

        // Print out the pattern being used for formatting
        // and the formatted output.
        System.out.println(mFmt.format(pattern, values));
        System.out.println(mFmt.getLocale());
    }
}

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

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

HOME | COPYRIGHT | CONTACT US