MessageFormat setFormat() method in Java with Example Last Updated : 22 Jan, 2020 Comments Improve Suggest changes Like Article Like Report 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 parameter: formatElementIndex: this is the particular index where new format element is going to be placed. newFormat: this is the new Format element which is going to be placed. Return Value: This method has nothing to return. Exception : This method throws ArrayIndexOutOfBoundsException if the formatElementIndex is out of bound. Below are the examples to illustrate the setFormat() 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) { // creating and initializing MessageFormat MessageFormat mf = new MessageFormat("{0, date, #}, {0, number, #.##}, {0, time}"); // display the current pattern System.out.println("old pattern : " + mf.toPattern()); // getting all the format element // used in MessageFormat Object Format[] formats = mf.getFormats(); // setting the new format element // at particular index // using setFormat() method for (int i = 0; i < formats.length; i++) mf.setFormat(i, formats[1]); // display the result System.out.println("\nnew pattern : " + mf.toPattern()); } } Output: old pattern : {0,date, #}, {0,number, #0.##}, {0,time} new pattern : {0,number, #0.##}, {0,number, #0.##}, {0,number, #0.##} 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 pattern System.out.println("old pattern : " + mf.toPattern()); // getting all the format element // used in MessageFormat Object Format[] formats = mf.getFormats(); // setting the new format element // at particular index // using setFormat() method for (int i = 0; i <= formats.length + 1; i++) mf.setFormat(-1, formats[1]); // display the result System.out.println("\nnew pattern : " + mf.toPattern()); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("\narray is out of bound"); System.out.println("Exception thrown : " + e); } } } Output: old pattern : {0,date, #}, {0,number, #0.##}, {0,time} array is out of bound Exception thrown : java.lang.ArrayIndexOutOfBoundsException: -1 Reference: https://docs.oracle.com/javase/9/docs/api/java/text/MessageFormat.html#setFormat-int-java.text.Format- Comment More infoAdvertise with us Next Article MessageFormat setFormat() method in Java with Example R rohitprasad3 Follow Improve Article Tags : Java Java-Functions Java-text package Java-MessageFormat Practice Tags : Java Similar Reads MessageFormat setFormats() method in Java with Example 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 o 3 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 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 Like