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

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

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

Java.text.MessageFormat类的setFormats()方法用于通过覆盖旧模式来设置消息格式对象模式中新格式元素的数组。

句法:

public void setFormats(Format[] newFormats)

参数:此方法将格式元素数组作为参数,用于覆盖消息格式对象的旧模式。
返回值:此方法没有任何返回值。
异常:如果新格式元素的数组为空,则此方法抛出NullPointerException

以下是说明setFormats()方法的示例:

示例 1:

Java
// Java program to demonstrate
// setFormats() 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, #}, {0, number, #.##}, {0, time}");
 
            // display the current message format object
            displayFormat(mf);
 
            // creating and initializing new Format object array
            Format[] format = { new SimpleDateFormat("YYYY-'W'ww-u"),
                                new DecimalFormat("0.##") };
 
            // setting the new array of formats
            // in the pattern of MessageFormat object
            // using setFormats() method
            mf.setFormats(format);
 
            // display the Override MessageFormat object
            System.out.println();
            displayFormat(mf);
        }
        catch (NullPointerException e) {
            System.out.println("format array is null");
            System.out.println("Exception thrown : " + e);
        }
    }
 
    // Defining displayFormat method
    public static void displayFormat(MessageFormat mf)
    {
 
        // 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("Required Formats are : ");
        for (int i = 0; i < formats.length; i++)
            System.out.println(formats[i]);
    }
}


Java
// Java program to demonstrate
// setFormats() 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, #}, {0, number, #.##}, {0, time}");
 
            // display the current message format object
            displayFormat(mf);
 
            // creating and initializing new Format object array
            Format[] format = { new SimpleDateFormat("YYYY-'W'ww-u"),
                                new DecimalFormat("0.##") };
 
            // setting the new array of formats
            // in the pattern of MessageFormat object
            // using setFormats() method
            mf.setFormats(null);
 
            // display the Override MessageFormat object
            System.out.println();
            displayFormat(mf);
        }
        catch (NullPointerException e) {
            System.out.println("\nformat array is null");
            System.out.println("Exception thrown : " + e);
        }
    }
 
    // Defining displayFormat method
    public static void displayFormat(MessageFormat mf)
    {
 
        // 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("Required 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@403
java.text.DecimalFormat@674fc
java.text.SimpleDateFormat@8400729

pattern : {0, date, YYYY-'W'ww-u}, {0, number, #0.##}, {0, time}
Required Formats are : 
java.text.SimpleDateFormat@d21287d2
java.text.DecimalFormat@674dc
java.text.SimpleDateFormat@8400729

示例 2:

Java

// Java program to demonstrate
// setFormats() 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, #}, {0, number, #.##}, {0, time}");
 
            // display the current message format object
            displayFormat(mf);
 
            // creating and initializing new Format object array
            Format[] format = { new SimpleDateFormat("YYYY-'W'ww-u"),
                                new DecimalFormat("0.##") };
 
            // setting the new array of formats
            // in the pattern of MessageFormat object
            // using setFormats() method
            mf.setFormats(null);
 
            // display the Override MessageFormat object
            System.out.println();
            displayFormat(mf);
        }
        catch (NullPointerException e) {
            System.out.println("\nformat array is null");
            System.out.println("Exception thrown : " + e);
        }
    }
 
    // Defining displayFormat method
    public static void displayFormat(MessageFormat mf)
    {
 
        // 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("Required 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@403
java.text.DecimalFormat@674fc
java.text.SimpleDateFormat@8400729

format array is null
Exception thrown : java.lang.NullPointerException

参考: https://docs.oracle.com/javase/9/docs/api/ Java/text/MessageFormat.html#setFormats-java.text.Format:A-