📌  相关文章
📜  Java中的 MessageFormat applyPattern() 方法与示例

📅  最后修改于: 2022-05-13 01:54:55.459000             🧑  作者: Mango

Java中的 MessageFormat applyPattern() 方法与示例

Java.text.MessageFormat类的applyPattern()方法用于通过覆盖当前的 FormatElement、FormatType 和 FormatStyle 来设置当前 MessageFormat 的新模式文本。句法:

public void applyPattern(String newPattern)

参数:此方法将字符串newPattern作为参数,这是此 MessageFormat 对象的新文本模式。返回值:此方法不返回任何内容。异常:如果指定的 newPattern 为 null,则此方法抛出NullPointerException 。以下是说明applyPattern()方法的示例:示例 1:

Java
// Java program to demonstrate
// applyPattern() 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, #}, {0, number, #.##}, {0, number}");
 
        // display the result
        System.out.println("old pattern : "
                           + mf.toPattern());
 
        // applying new string pattern
        // to this MessageFormat Object
        // using applyPattern() method
        mf.applyPattern("{0, time, #}");
 
        // display the result
        System.out.println("\nnew pattern : "
                           + mf.toPattern());
    }
}


Java
// Java program to demonstrate
// applyPattern() method
 
import java.text.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        try {
            // creating and initializing MessageFormat
            MessageFormat mf
                = new MessageFormat("{0, date, #}, {1, time, #}, {0, number}");
 
            // display the result
            System.out.println("old pattern : "
                               + mf.toPattern());
 
            // applying new string pattern
            // to this MessageFormat Object
            // using applyPattern() method
            mf.applyPattern(null);
 
            // display the result
            System.out.println("\nnew pattern : "
                               + mf.toPattern());
        }
        catch (NullPointerException e) {
            System.out.println("\nString is Null");
            System.out.println("Exception thrown : " + e);
        }
    }
}


输出:
old pattern : {0, number, #}, {0, number, #0.##}, {0, number}

new pattern : {0, date, #}

示例 2:

Java

// Java program to demonstrate
// applyPattern() method
 
import java.text.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        try {
            // creating and initializing MessageFormat
            MessageFormat mf
                = new MessageFormat("{0, date, #}, {1, time, #}, {0, number}");
 
            // display the result
            System.out.println("old pattern : "
                               + mf.toPattern());
 
            // applying new string pattern
            // to this MessageFormat Object
            // using applyPattern() method
            mf.applyPattern(null);
 
            // display the result
            System.out.println("\nnew pattern : "
                               + mf.toPattern());
        }
        catch (NullPointerException e) {
            System.out.println("\nString is Null");
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
old pattern : {0, date, #}, {1, date, #}, {0, number}

String is Null
Exception thrown : java.lang.NullPointerException

参考: https://docs.oracle.com/javase/9/docs/api/ Java/text/MessageFormat.html#applyPattern-java.lang.String-