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
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
MessageFormat mf
= new MessageFormat( "{1, date, #}, {3, number, #.##}, {5, time}" );
System.out.println( "old pattern : "
+ mf.toPattern());
Format[] formats = mf.getFormatsByArgumentIndex();
for ( int i = 0 ; i < formats.length; i++)
mf.setFormatByArgumentIndex(i, formats[ 1 ]);
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
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
MessageFormat mf
= new MessageFormat( "{1, date, #}, {3, number, #.##}, {5, time}" );
System.out.println( "old pattern : "
+ mf.toPattern());
Format fm = NumberFormat.getInstance();
for ( int i = 0 ; i < 6 ; i++)
mf.setFormatByArgumentIndex(i, fm);
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-
Last Updated :
25 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...