當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java MessageFormat setFormatsByArgumentIndex()用法及代碼示例


java.text.MessageFormat類的setFormatsByArgumentIndex方法用於通過覆蓋舊模式來設置消息格式對象模式中新格式元素的數組。

用法:

public void setFormatsByArgumentIndex(Format[] newFormats)

參數:此方法采用格式元素數組作為參數,以覆蓋郵件格式對象的舊模式。


返回值:此方法無返回值。

異常:如果新格式元素的數組為null,則此方法引發NullPointerException。

下麵是說明setFormatsByArgumentIndex()方法的示例:

範例1:

// Java program to demonstrate 
// setFormatsByArgumentIndex() 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, #}, {2, number, #.##}, {4, 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("10.5678") }; 
  
            // settting the new array of formats 
            // in the pattern of MessageFormat object 
            // using setFormatsByArgumentIndex() method 
            mf.setFormatsByArgumentIndex(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]); 
    } 
}
輸出:
pattern:{0, date, #}, {2, number, #0.##}, {4, time}
Required Formats are:
java.text.SimpleDateFormat@403
java.text.DecimalFormat@674fc
java.text.SimpleDateFormat@8400729

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

範例2:

// Java program to demonstrate 
// setFormatsByArgumentIndex() 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, #}, {2, number, #.##}, {4, 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("10.5678") }; 
  
            // settting the new array of formats 
            // in the pattern of MessageFormat object 
            // using setFormatsByArgumentIndex() method 
            mf.setFormatsByArgumentIndex(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, #}, {2, number, #0.##}, {4, 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#setFormatsByArgumentIndex-java.text.Format:A-



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 MessageFormat setFormatsByArgumentIndex() method in Java with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。