以下程序代碼顯示瞭如何格式化的示例;基於類java.text.MessageFormat
的字符串和數值。請參閱說明類,然後調整日期格式rrrr-mm-dd
。以java格式化結果
這是代碼:
import java.text.MessageFormat;
import java.util.Date;
public class FormattingResults {
public static void main(String[] args){
String s;
Date now = new Date();
String place = "Kraków";
int mile = 1852;
double tax = 0.22;
double price = 31560.76;
s = MessageFormat.format("{0}, {1,date,full}, time. {1,time,short}",
place, now);
System.out.println(s);
s = MessageFormat.format("{0}kB to {1}B", 256,256*1024);
System.out.println(s);
s = MessageFormat.format("{0} x {0} x {0} = {1}", 7, 7*7*7);
System.out.println(s);
s = MessageFormat.format("{0,number,currency} + tax({1,number,percent})",
price, tax);
System.out.println(s);
s = MessageFormat.format("{0} {1} it's approximately {2}m", 1,
"nautical mile", mile);
System.out.println(s);
}
}
在一般情況下,我不明白這個代碼,可能有人幫我解決一個很簡單的任務,例如:
"("{0}, {1,date,full}, time. {1,time,short}",
我讀Oracle文檔,但是窗臺不知道它是如何工作的。
請查看[當某人回答我的問題時該怎麼辦?](http://stackoverflow.com/help/someone-answers)。謝謝。 @ D1l4x –