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()

Paramter: This method does not take any argument as a parameter.

Return Value: This method returns string representation of the current pattern of this message format object.

Below are the examples to illustrate the toPattern() method:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// toPattern() 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, #}, {2, date, #.#}, {4, time}");
  
        // getting the pattern
        // of this MessageFormat Object
        // using toPattern() method
        String patt = mf.toPattern();
  
        // display the result
        System.out.println("current pattern of MessageFormat Object : " + patt);
    }
}

chevron_right


Output:

current pattern of MessageFormat Object : {0, number, #}, {2, date, #.#}, {4, time}

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// toPattern() 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, #}, {4, time}");
  
        // getting the pattern
        // of this MessageFormat Object
        // using toPattern() method
        String patt = mf.toPattern();
  
        // display the result
        System.out.println("current pattern of MessageFormat Object : " + patt);
    }
}

chevron_right


Output:

current pattern of MessageFormat Object : {0, number, #}, {4, time}

Reference: https://docs.oracle.com/javase/9/docs/api/java/text/MessageFormat.html#toPattern–

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.