MessageFormat applyPattern() method in Java with Example Last Updated : 27 Apr, 2022 Comments Improve Suggest changes Like Article Like Report The applyPattern() method of java.text.MessageFormat class is used to set the new pattern text for current MessageFormat by overriding the current FormatElement, FormatType and FormatStyle. Syntax: public void applyPattern(String newPattern) Parameter: This method takes string newPattern as parameter which is the new text pattern for this MessageFormat object. Return Value: This method returns nothing. Exception: This method throws NullPointerException if the specified newPattern is null. Below are the examples to illustrate the applyPattern() method: Example 1: Java // Java program to demonstrate // applyPattern() 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, number, #}, {0, number, #.##}, {0, number}"); // display the result System.out.println("old pattern : " + mf.toPattern()); // applying new string pattern // to this MessageFormat Object // using applyPattern() method mf.applyPattern("{0, time, #}"); // display the result System.out.println("\nnew pattern : " + mf.toPattern()); } } Output:old pattern : {0, number, #}, {0, number, #0.##}, {0, number} new pattern : {0, date, #} Example 2: Java // Java program to demonstrate // applyPattern() 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, #}, {1, time, #}, {0, number}"); // display the result System.out.println("old pattern : " + mf.toPattern()); // applying new string pattern // to this MessageFormat Object // using applyPattern() method mf.applyPattern(null); // display the result System.out.println("\nnew pattern : " + mf.toPattern()); } catch (NullPointerException e) { System.out.println("\nString is Null"); System.out.println("Exception thrown : " + e); } } } Output:old pattern : {0, date, #}, {1, date, #}, {0, number} String is Null Exception thrown : java.lang.NullPointerException Reference: https://docs.oracle.com/javase/9/docs/api/java/text/MessageFormat.html#applyPattern-java.lang.String- Comment More infoAdvertise with us Next Article MessageFormat applyPattern() method in Java with Example R rohitprasad3 Follow Improve Article Tags : Java Java-Functions Java-text package Java-MessageFormat Practice Tags : Java Similar Reads SimpleDateFormat applyPattern() Method in Java with Examples The applyPattern() Method of SimpleDateFormat class is used to set a given defined pattern to the Date Format. It simply converts a particular date and time to a specific format as defined by the user for eg., dd/ MM/ yyyy HH:mm Z or MM/ dd/ yyyy HH:mm Z.Syntax: public void applyPattern(String patte 2 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 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 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 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 Like