Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

MessageFormat getFormats() method in Java with Example

  • Last Updated : 17 Jan, 2020

The getFormats() method of java.text.MessageFormat class is used to get the formats used for previously set pattern for the message format object.

Syntax:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

public Format[] getFormats()

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

Return Value: This method returns format used for previously set pattern in the message format object.



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

Example 1:




// Java program to demonstrate
// getFormats() 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 result
        System.out.println("pattern : "
                           + mf.toPattern());
  
        // getting all format
        // used in MessageFormat Object
        // using getFormats() method
        Format[] formats = mf.getFormats();
  
        // display the result
        System.out.println("\nRequired Formats are : ");
        for (int i = 0; i < formats.length; i++)
            System.out.println(formats[i]);
    }
}
Output:
pattern : {0, date, #}, {0, number, #0.##}, {0, time}

Required Formats are : 
java.text.SimpleDateFormat@23
java.text.DecimalFormat@674dc
java.text.SimpleDateFormat@8400729

Example 2:




// Java program to demonstrate
// getFormats() 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("{1, number, integer}");
  
        // display the result
        System.out.println("pattern : "
                           + mf.toPattern());
  
        // getting all format
        // used in MessageFormat Object
        // using getFormats() method
        Format[] formats = mf.getFormats();
  
        // display the result
        System.out.println("\nRequired Formats are : ");
        for (int i = 0; i < formats.length; i++)
            System.out.println(formats[i]);
    }
}
Output:
pattern : {1, number, integer}

Required Formats are : 
java.text.DecimalFormat@674dc

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :