1 - 9 : MessageFormatサンプル

1 - 9 : MessageFormatサンプル

java.text.MessageFormatのサンプルです。MessageFormat.formatの第1引数にフォーマットを渡し、第2引数に値が入ったObject配列を渡すと書式通りにフォーマットした文字列を取得します。

[MsgFormat.java]

import java.text.MessageFormat;

import java.util.Date;

class MsgFormat {

public static void main(String args[]){

// 表示対象 オブジェクト配列

Object[] arguments = {

new Integer(7), // 年齢

new Date(System.currentTimeMillis()), // 現在日時

""Tom"", // 名前

new Double(1234.9) // パラメータ

};

Double dbl = new Double(8989.56);

String myString = MessageFormat.format(

""名前 : {2}\n年齢 : {0,number,0000}\n"" +

""現在日時 : {1,date} {1,time}\nパラメータ : {3,number,0000.0000}"",

arguments);

System.out.println(myString);

}

}

実行結果

名前 : Tom

年齢 : 0007

現在日時 : 2003/01/19 11:16:11

パラメータ : 1234.9000