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

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

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

Java.text.MessageFormat类的setFormatByArgumentIndex()方法用于通过覆盖先前的模式在消息格式对象的模式中的特定索引处设置新的格式元素。

句法:

public void setFormatByArgumentIndex(int argumentIndex,
                                     Format newFormat)

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

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

返回值:此方法没有任何返回值。
以下是说明setFormatByArgumentIndex()方法的示例:
示例 1:

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


Java
// Java program to demonstrate
// setFormatByArgumentIndex() 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, date, #}, {3, number, #.##}, {5, time}");
 
        // display the current pattern
        System.out.println("old pattern : "
                           + mf.toPattern());
 
        // creating and initializing new Format element
        Format fm = NumberFormat.getInstance();
 
        // setting the new format element
        // at particular index
        // using setFormatByArgumentIndex() method
        for (int i = 0; i < 6; i++)
            mf.setFormatByArgumentIndex(i, fm);
 
        // display the result
        System.out.println("\nnew pattern : "
                           + mf.toPattern());
    }
}


输出:
old pattern : {1,date, #}, {3,number, #0.##}, {5,time}

new pattern : {1,date, #}, {3,date, #}, {5,date, #}

示例 2:

Java

// Java program to demonstrate
// setFormatByArgumentIndex() 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, date, #}, {3, number, #.##}, {5, time}");
 
        // display the current pattern
        System.out.println("old pattern : "
                           + mf.toPattern());
 
        // creating and initializing new Format element
        Format fm = NumberFormat.getInstance();
 
        // setting the new format element
        // at particular index
        // using setFormatByArgumentIndex() method
        for (int i = 0; i < 6; i++)
            mf.setFormatByArgumentIndex(i, fm);
 
        // display the result
        System.out.println("\nnew pattern : "
                           + mf.toPattern());
    }
}
输出:
old pattern : {1,date, #}, {3,number, #0.##}, {5,time}

new pattern : {1,number}, {3,number}, {5,number}

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