String message = "Once upon a time ({1,date}, around about {1,time,short}), there " + "was a humble developer named Geppetto who slaved for " + "{0,number,integer} days with {2,number,percent} complete user " + "requirements. "; Object[ ] variables = new Object[ ] { new Integer(4), new Date( ), new Double(0.21) } String output = MessageFormat.format( message, variables ); System.out.println(output); 隐藏在信息中的是描述输出的格式的一种短小的代码,范例的输出如下:
Once upon a time (Nov 3, 2002, around about 1:35 AM), there was a humble developer named Geppetto who slaved for 4 days with 21% complete user requirements. 假如相同的信息需要被重复输出但是变量的值不同,那么创建一个MessageFormat对象并给出信息。下面是上面的例子的修正版: