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對象并給出信息。下面是上面的例子的修正版: