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

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

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

Java.text.MessageFormat类的setFormats()方法用于在此消息格式对象的模式中的特定索引处设置新的格式元素。

句法:

public void setFormat(int formatElementIndex,
                      Format newFormat)

参数:此方法将以下参数作为参数:

  • formatElementIndex:这是要放置新格式元素的特定索引。
  • newFormat:这是要放置的新格式元素。

返回值:此方法没有任何返回值。

异常:如果 formatElementIndex 超出范围,此方法将引发ArrayIndexOutOfBoundsException

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

示例 1:

// Java program to demonstrate
// setFormats() 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 current pattern
        System.out.println("old pattern : "
                           + mf.toPattern());
  
        // getting all the format element
        // used in MessageFormat Object
        Format[] formats = mf.getFormats();
  
        // setting the new format element
        // at particular index
        // using setFormat() method
        for (int i = 0; i < formats.length; i++)
            mf.setFormat(i, formats[1]);
  
        // display the result
        System.out.println("\nnew pattern : "
                           + mf.toPattern());
    }
}
输出:
old pattern : {0,date, #}, {0,number, #0.##}, {0,time}

new pattern : {0,number, #0.##}, {0,number, #0.##}, {0,number, #0.##}

示例 2:

// 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 pattern
            System.out.println("old pattern : "
                               + mf.toPattern());
  
            // getting all the format element
            // used in MessageFormat Object
            Format[] formats = mf.getFormats();
  
            // setting the new format element
            // at particular index
            // using setFormat() method
            for (int i = 0; i <= formats.length + 1; i++)
                mf.setFormat(-1, formats[1]);
  
            // display the result
            System.out.println("\nnew pattern : "
                               + mf.toPattern());
        }
        catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("\narray is out of bound");
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
old pattern : {0,date, #}, {0,number, #0.##}, {0,time}

array is out of bound
Exception thrown : java.lang.ArrayIndexOutOfBoundsException: -1

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