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 pervious 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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-

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.