MessageFormat setFormatsByArgumentIndex() method in Java with Example
Last Updated :
22 Jan, 2020
The setFormatsByArgumentIndex method of java.text.MessageFormat class is used to set the array of new format element in the pattern of message format object by overriding the old pattern.
Syntax:
public void setFormatsByArgumentIndex(Format[] newFormats)
Parameter: This method takes an array of format element as a parameter for overriding the old pattern of message format object.
Return Value: This method has nothing to return.
Exception: This method throws NullPointerException if array of new format element is null.
Below are the examples to illustrate the setFormatsByArgumentIndex() method:
Example 1:
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
MessageFormat mf
= new MessageFormat( "{0, date, #}, {2, number, #.##}, {4, time}" );
displayFormat(mf);
Format[] format = { new SimpleDateFormat( "YYYY-'W'ww-u" ),
new DecimalFormat( "10.5678" ) };
mf.setFormatsByArgumentIndex(format);
System.out.println();
displayFormat(mf);
}
catch (NullPointerException e) {
System.out.println( "format array is null" );
System.out.println( "Exception thrown : " + e);
}
}
public static void displayFormat(MessageFormat mf)
{
System.out.println( "pattern : "
+ mf.toPattern());
Format[] formats = mf.getFormats();
System.out.println( "Required Formats are : " );
for ( int i = 0 ; i < formats.length; i++)
System.out.println(formats[i]);
}
}
|
Output:
pattern : {0, date, #}, {2, number, #0.##}, {4, time}
Required Formats are :
java.text.SimpleDateFormat@403
java.text.DecimalFormat@674fc
java.text.SimpleDateFormat@8400729
pattern : {0, date, YYYY-'W'ww-u}, {2, number, #0.##}, {4, time}
Required Formats are :
java.text.SimpleDateFormat@d21287d2
java.text.DecimalFormat@674fc
java.text.SimpleDateFormat@8400729
Example 2:
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
MessageFormat mf
= new MessageFormat( "{0, date, #}, {2, number, #.##}, {4, time}" );
displayFormat(mf);
Format[] format = { new SimpleDateFormat( "YYYY-'W'ww-u" ),
new DecimalFormat( "10.5678" ) };
mf.setFormatsByArgumentIndex( null );
System.out.println();
displayFormat(mf);
}
catch (NullPointerException e) {
System.out.println( "\nformat array is null" );
System.out.println( "Exception thrown : " + e);
}
}
public static void displayFormat(MessageFormat mf)
{
System.out.println( "pattern : "
+ mf.toPattern());
Format[] formats = mf.getFormats();
System.out.println( "Required Formats are : " );
for ( int i = 0 ; i < formats.length; i++)
System.out.println(formats[i]);
}
}
|
Output:
pattern : {0, date, #}, {2, number, #0.##}, {4, time}
Required Formats are :
java.text.SimpleDateFormat@403
java.text.DecimalFormat@674fc
java.text.SimpleDateFormat@8400729
format array is null
Exception thrown : java.lang.NullPointerException
Reference: https://docs.oracle.com/javase/9/docs/api/java/text/MessageFormat.html#setFormatsByArgumentIndex-java.text.Format:A-
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...