Java中的 MessageFormat getFormatsByArgumentIndex() 方法示例
Java.text.MessageFormat类的getFormats()方法用于按时间顺序获取消息格式对象模式中存在的每个参数索引的格式。如果该特定参数索引不存在格式,它将仅返回 null。
句法:
public Format[] getFormatsByArgumentIndex()
参数:此方法不接受任何参数作为参数。
返回值:此方法返回按时间顺序出现在消息格式对象模式中的每个参数索引的格式。
下面是说明getFormatsByArgumentIndex()方法的示例:
示例 1:
Java
// Java program to demonstrate
// getFormats() 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, number, integer}, {2, number, float}, {5, date}");
// display the result
System.out.println("pattern : "
+ mf.toPattern());
// getting all the format
// used in MessageFormat Object
// using getFormatsByArgumentIndex() method
Format[] formats = mf.getFormatsByArgumentIndex();
// display the result
System.out.println("\nRequired Formats are : ");
for (int i = 0; i < formats.length; i++)
System.out.println(formats[i]);
}
}
Java
// Java program to demonstrate
// getFormats() 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("{0, number, #}, {2, date, #.#}, {4, time}");
// display the result
System.out.println("pattern : "
+ mf.toPattern());
// getting all the format
// used in MessageFormat Object
// using getFormatsByArgumentIndex() method
Format[] formats = mf.getFormatsByArgumentIndex();
// display the result
System.out.println("\nRequired Formats are : ");
for (int i = 0; i < formats.length; i++)
System.out.println(formats[i]);
}
}
输出:
pattern : {1, number, integer}, {2, number, float#}, {5, date}
Required Formats are :
null
java.text.DecimalFormat@674dc
java.text.DecimalFormat@5d69738
null
null
java.text.SimpleDateFormat@ce9bf0a5
示例 2:
Java
// Java program to demonstrate
// getFormats() 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("{0, number, #}, {2, date, #.#}, {4, time}");
// display the result
System.out.println("pattern : "
+ mf.toPattern());
// getting all the format
// used in MessageFormat Object
// using getFormatsByArgumentIndex() method
Format[] formats = mf.getFormatsByArgumentIndex();
// display the result
System.out.println("\nRequired Formats are : ");
for (int i = 0; i < formats.length; i++)
System.out.println(formats[i]);
}
}
输出:
pattern : {0, number, #}, {2, date, #.#}, {4, time}
Required Formats are :
java.text.DecimalFormat@674dc
null
java.text.SimpleDateFormat@8918
null
java.text.SimpleDateFormat@8400729
参考: https://docs.oracle.com/javase/9/docs/api/ Java/text/MessageFormat.html#getFormatsByArgumentIndex–