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() methodimport 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()); }} |
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() methodimport 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()); }} |
old pattern : {1,date, #}, {3,number, #0.##}, {5,time}
new pattern : {1,number}, {3,number}, {5,number}
