MessageFormat setFormats() method in Java with Example Last Updated : 16 Aug, 2021 Comments Improve Suggest changes Like Article Like Report The setFormats() method of java.text.MessageFormat class is used to set the array of new format element in the pattern of message format object by overriding the old pattern. Syntax: public void setFormats(Format[] newFormats) Parameter: This method takes array of format element as a parameter for overriding the old pattern of message format object.Return Value: This method has nothing to return.Exception: This method throws NullPointerException if array of new format element is null. Below are the examples to illustrate the setFormats() method: Example 1: Java // Java program to demonstrate // setFormats() method import java.text.*; import java.util.*; import java.io.*; public class GFG { public static void main(String[] argv) { try { // creating and initializing MessageFormat MessageFormat mf = new MessageFormat("{0, date, #}, {0, number, #.##}, {0, time}"); // display the current message format object displayFormat(mf); // creating and initializing new Format object array Format[] format = { new SimpleDateFormat("YYYY-'W'ww-u"), new DecimalFormat("0.##") }; // setting the new array of formats // in the pattern of MessageFormat object // using setFormats() method mf.setFormats(format); // display the Override MessageFormat object System.out.println(); displayFormat(mf); } catch (NullPointerException e) { System.out.println("format array is null"); System.out.println("Exception thrown : " + e); } } // Defining displayFormat method public static void displayFormat(MessageFormat mf) { // display the result System.out.println("pattern : " + mf.toPattern()); // getting all format // used in MessageFormat Object // using getFormats() method Format[] formats = mf.getFormats(); // display the result System.out.println("Required Formats are : "); for (int i = 0; i < formats.length; i++) System.out.println(formats[i]); } } Output: pattern : {0, date, #}, {0, number, #0.##}, {0, time} Required Formats are : java.text.SimpleDateFormat@403 java.text.DecimalFormat@674fc java.text.SimpleDateFormat@8400729 pattern : {0, date, YYYY-'W'ww-u}, {0, number, #0.##}, {0, time} Required Formats are : java.text.SimpleDateFormat@d21287d2 java.text.DecimalFormat@674dc java.text.SimpleDateFormat@8400729 Example 2: Java // Java program to demonstrate // setFormats() method import java.text.*; import java.util.*; import java.io.*; public class GFG { public static void main(String[] argv) { try { // creating and initializing MessageFormat MessageFormat mf = new MessageFormat("{0, date, #}, {0, number, #.##}, {0, time}"); // display the current message format object displayFormat(mf); // creating and initializing new Format object array Format[] format = { new SimpleDateFormat("YYYY-'W'ww-u"), new DecimalFormat("0.##") }; // setting the new array of formats // in the pattern of MessageFormat object // using setFormats() method mf.setFormats(null); // display the Override MessageFormat object System.out.println(); displayFormat(mf); } catch (NullPointerException e) { System.out.println("\nformat array is null"); System.out.println("Exception thrown : " + e); } } // Defining displayFormat method public static void displayFormat(MessageFormat mf) { // display the result System.out.println("pattern : " + mf.toPattern()); // getting all format // used in MessageFormat Object // using getFormats() method Format[] formats = mf.getFormats(); // display the result System.out.println("Required Formats are : "); for (int i = 0; i < formats.length; i++) System.out.println(formats[i]); } } Output: pattern : {0, date, #}, {0, number, #0.##}, {0, time} Required Formats are : java.text.SimpleDateFormat@403 java.text.DecimalFormat@674fc java.text.SimpleDateFormat@8400729 format array is null Exception thrown : java.lang.NullPointerException Reference: https://docs.oracle.com/javase/9/docs/api/java/text/MessageFormat.html#setFormats-java.text.Format:A- Comment More infoAdvertise with us Next Article MessageFormat setFormats() method in Java with Example rohitprasad3 Follow Improve Article Tags : Java Java-Functions Java-text package Java-MessageFormat Practice Tags : Java Similar Reads MessageFormat setFormat() method in Java with Example The setFormats() method of java.text.MessageFormat class is used to set the new format element at the particular index in the pattern of this message format object. Syntax: public void setFormat(int formatElementIndex, Format newFormat) Parameters: This method takes the following argument as a param 2 min read MessageFormat setLocale() method in Java with Example The setLocale() method of java.text.MessageFormat class is used to set the new locale in this message format object. Syntax: public void setLocale(Locale locale) Parameter: This method takes new locale object as a parameter. Return Value: This method has nothing to return. Below are the examples to 2 min read MessageFormat toPattern() method in Java with Example The toPattern() method of java.text.MessageFormat class is used to get string representation of the current pattern of this message format object.Syntax: public String toPattern() Parameter: This method does not take any argument as a parameter.Return Value: This method returns string representation 2 min read MessageFormat parseObject() method in Java with Example The parseObject() method of java.text.MessageFormat class is used to parse the string object starting from the passed parse position in the parseObject() method.Syntax: public Object parseObject(String source, ParsePosition pos) Parameter: This method takes the following arguments as parameter. sour 2 min read MessageFormat setFormatsByArgumentIndex() method in Java with Example The setFormatsByArgumentIndex method of java.text.MessageFormat class is used to set the array of new format element in the pattern of message format object by overriding the old pattern. Syntax: public void setFormatsByArgumentIndex(Format[] newFormats) Parameter: This method takes an array of form 3 min read MessageFormat setFormatByArgumentIndex() method in Java with Example The setFormatByArgumentIndex() method of java.text.MessageFormat class is used to set the new format element at the particular index in pattern of message format object by overriding the previous pattern. Syntax: public void setFormatByArgumentIndex(int argumentIndex, Format newFormat) Parameters: T 2 min read MessageFormat parse() method in Java with Example : Set - 1 The parse() method of java.text.MessageFormat class is used to parse the string object from the beginning of the index.Syntax: public Object[] parse(String source) throws ParseException Parameter: This method takes string source as an argument over which parsing is going to take place.Return Value: 2 min read MessageFormat parse() method in Java with Example : Set - 2 The parse() method of java.text.MessageFormat class is used to parse the string object starting from the passed parse position in the parse() method.Syntax: public Object[] parse(String source, ParsePosition pos) Parameter: This method takes the following arguments as parameter. source :- string ove 2 min read MessageFormat format() method in Java with Example : Set - 1 The format() method of java.text.MessageFormat class is used to get the formatted array of object appended into the string buffer object. formatted array will contain all forms of element lies in the pattern of MessageFormat object.Syntax: public final StringBuffer format(Object[] arguments, StringB 3 min read MessageFormat format() method in Java with Example : Set - 2 The format() method of java.text.MessageFormat class is used to get the formatted array of object according to the specified pattern of message format object. new string pattern will be considered while performing the action.Syntax: public static String format(String pattern, Object... arguments) Pa 2 min read Like