MessageFormat applyPattern() method in Java with Example

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)

Paramter: 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
    }
}

chevron_right


Output:

old pattern : {0, number, #}, {0, number, #0.##}, {0, number}

new pattern : {0, date, #}

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


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-

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.