Saturday, October 25, 2025
HomeLanguagesJavaMessageFormat setFormatByArgumentIndex() method in Java with Example

MessageFormat setFormatByArgumentIndex() method in Java with Example

The setFormatByArgumentIndex() method of java.text.MessageFormat class is used to set the new format element at the particular index in pattern of message format object by overriding the previous pattern.
 

Syntax:  

public void setFormatByArgumentIndex(int argumentIndex,
                                     Format newFormat)

Parameters: This method takes the following argument as a parameter.  

  • argumentIndex :- this is the particular index where new format element is going to be placed.
  • newFormat :- this is the new Format element which is going to be placed.

Return Value: This method has nothing to return.
Below are the examples to illustrate the setFormatByArgumentIndex() method:
Example 1:  

Java




// Java program to demonstrate
// setFormatByArgumentIndex() 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, date, #}, {3, number, #.##}, {5, time}");
 
        // display the current pattern
        System.out.println("old pattern : "
                           + mf.toPattern());
 
        // getting all the format element
        // used in MessageFormat Object
        Format[] formats = mf.getFormatsByArgumentIndex();
 
        // setting the new format element
        // at particular index
        // using setFormatByArgumentIndex() method
        for (int i = 0; i < formats.length; i++)
            mf.setFormatByArgumentIndex(i, formats[1]);
 
        // display the result
        System.out.println("\nnew pattern : "
                           + mf.toPattern());
    }
}


Output: 

old pattern : {1,date, #}, {3,number, #0.##}, {5,time}

new pattern : {1,date, #}, {3,date, #}, {5,date, #}

 

Example 2: 

Java




// Java program to demonstrate
// setFormatByArgumentIndex() 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, date, #}, {3, number, #.##}, {5, time}");
 
        // display the current pattern
        System.out.println("old pattern : "
                           + mf.toPattern());
 
        // creating and initializing new Format element
        Format fm = NumberFormat.getInstance();
 
        // setting the new format element
        // at particular index
        // using setFormatByArgumentIndex() method
        for (int i = 0; i < 6; i++)
            mf.setFormatByArgumentIndex(i, fm);
 
        // display the result
        System.out.println("\nnew pattern : "
                           + mf.toPattern());
    }
}


Output: 

old pattern : {1,date, #}, {3,number, #0.##}, {5,time}

new pattern : {1,number}, {3,number}, {5,number}

 

Reference: https://docs.oracle.com/javase/9/docs/api/java/text/MessageFormat.html#setFormatByArgumentIndex-int-java.text.Format-

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS