Java中的 MessageFormat getFormats() 方法与示例
Java.text.MessageFormat类的getFormats()方法用于获取用于消息格式对象先前设置的模式的格式。
句法:
public Format[] getFormats()
参数:此方法不接受任何参数作为参数。
返回值:此方法返回用于消息格式对象中先前设置的模式的格式。
以下是说明getFormats()方法的示例:
示例 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("{0, date, #}, {0, number, #.##}, {0, time}");
// display the result
System.out.println("pattern : "
+ mf.toPattern());
// getting all format
// used in MessageFormat Object
// using getFormats() method
Format[] formats = mf.getFormats();
// 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("{1, number, integer}");
// display the result
System.out.println("pattern : "
+ mf.toPattern());
// getting all format
// used in MessageFormat Object
// using getFormats() method
Format[] formats = mf.getFormats();
// display the result
System.out.println("\nRequired Formats are : ");
for (int i = 0; i < formats.length; i++)
System.out.println(formats[i]);
}
}
输出:
pattern : {0, date, #}, {0, number, #0.##}, {0, time}
Required Formats are :
java.text.SimpleDateFormat@23
java.text.DecimalFormat@674dc
java.text.SimpleDateFormat@8400729
示例 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("{1, number, integer}");
// display the result
System.out.println("pattern : "
+ mf.toPattern());
// getting all format
// used in MessageFormat Object
// using getFormats() method
Format[] formats = mf.getFormats();
// 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}
Required Formats are :
java.text.DecimalFormat@674dc
参考: https://docs.oracle.com/javase/9/docs/api/ Java/text/MessageFormat.html#getFormats–