Thursday, June 11, 2026
HomeLanguagesJavaMessageFormat toPattern() method in Java with Example

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 of the current pattern of this message format object.
Below are the examples to illustrate the toPattern() method:
Example 1: 
 

Java




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


Output: 

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

 

Example 2: 
 

Java




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


Output: 

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

 

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

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS