HomeLanguagesJavaMessageFormat getFormatsByArgumentIndex() method in Java with Example

MessageFormat getFormatsByArgumentIndex() method in Java with Example

The getFormatsByArgumentIndex() method of java.text.MessageFormat class is used to get the format for every argument index chronologically which is present in the pattern of message format object . if there is no format present for that particular argument index it will just return null.
Syntax: 
 

public Format[] getFormatsByArgumentIndex()

Parameter: This method does not take any argument as a parameter.
Return Value: This method returns format for every argument index which is present in the pattern of message format object chronologically.
Below are the examples to illustrate the getFormatsByArgumentIndex() method:
Example 1: 
 

Java




// Java program to demonstrate
// getFormatsByArgumentIndex() 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}, {2, number, float}, {5, date}");
 
        // display the result
        System.out.println("pattern : "
                           + mf.toPattern());
 
        // getting all the format
        // used in MessageFormat Object
        // using getFormatsByArgumentIndex() method
        Format[] formats = mf.getFormatsByArgumentIndex();
 
        // 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}, {2, number, float#}, {5, date}

Required Formats are : 
null
java.text.DecimalFormat@674dc
java.text.DecimalFormat@5d69738
null
null
java.text.SimpleDateFormat@ce9bf0a5

 

Example 2: 
 

Java




// Java program to demonstrate
// getFormatsByArgumentIndex() 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}");
 
        // display the result
        System.out.println("pattern : "
                           + mf.toPattern());
 
        // getting all the format
        // used in MessageFormat Object
        // using getFormatsByArgumentIndex() method
        Format[] formats = mf.getFormatsByArgumentIndex();
 
        // 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, number, #}, {2, date, #.#}, {4, time}

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

 

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

RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32646 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7020 POSTS0 COMMENTS
Nicole Veronica
12155 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12253 POSTS0 COMMENTS
Shaida Kate Naidoo
7152 POSTS0 COMMENTS
Ted Musemwa
7418 POSTS0 COMMENTS
Thapelo Manthata
7109 POSTS0 COMMENTS
Umr Jansen
7116 POSTS0 COMMENTS