Java中的 MessageFormat toPattern() 方法与示例
Java.text.MessageFormat类的toPattern()方法用于获取此消息格式对象的当前模式的字符串表示形式。
句法:
public String toPattern()
参数:此方法不接受任何参数作为参数。
返回值:此方法返回此消息格式对象的当前模式的字符串表示形式。
下面是说明toPattern()方法的示例:
示例 1:
Java
// Java program to demonstrate
// toPattern() 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}");
// getting the pattern
// of this MessageFormat Object
// using toPattern() method
String patt = mf.toPattern();
// display the result
System.out.println("current pattern of MessageFormat Object : " + patt);
}
}
Java
// Java program to demonstrate
// toPattern() 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, #}, {4, time}");
// getting the pattern
// of this MessageFormat Object
// using toPattern() method
String patt = mf.toPattern();
// display the result
System.out.println("current pattern of MessageFormat Object : " + patt);
}
}
输出:
current pattern of MessageFormat Object : {0, number, #}, {2, date, #.#}, {4, time}
示例 2:
Java
// Java program to demonstrate
// toPattern() 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, #}, {4, time}");
// getting the pattern
// of this MessageFormat Object
// using toPattern() method
String patt = mf.toPattern();
// display the result
System.out.println("current pattern of MessageFormat Object : " + patt);
}
}
输出:
current pattern of MessageFormat Object : {0, number, #}, {4, time}
参考: https://docs.oracle.com/javase/9/docs/api/ Java/text/MessageFormat.html#toPattern–