TestFormat.java import java.text.DateFormat; import java.text.DecimalFormat; import java.text.NumberFormat; import java.text.SimpleDateFormat; import java.util.Date; public class TestFormat { public static void main(String[] args) { defaultNumberFormat(); System.out.println(); customNumberFormat(); System.out.println(); defaultDateFormat(); System.out.println(); customDateFormat(); System.out.println(); } public static void defaultNumberFormat() { int i = 123456; double x = 882323.23523; double p = 0.528; double c = 52.83; NumberFormat nf = NumberFormat.getInstance(); System.out.println("NumberFormat.getInstance()"); System.out.println("Integer " + i + " is displayed as " + nf.format(i)); System.out.println("Double " + x + " is displayed as " + nf.format(x)); NumberFormat nfInt = NumberFormat.getIntegerInstance(); System.out.println("NumberFormat.getIntegerInstance()"); System.out.println("Integer " + i + " is displayed as " + nfInt.format(i)); NumberFormat nfNumber = NumberFormat.getNumberInstance(); System.out.println("NumberFormat.getNumberInstance()"); System.out.println("Double " + x + " is displayed as " + nfNumber.format(x)); NumberFormat nfPercent = NumberFormat.getPercentInstance(); System.out.println("NumberFormat.getPercentInstance()"); System.out.println("Percent " + p + " is displayed as " + nfPercent.format(p)); NumberFormat nfCurrency = NumberFormat.getCurrencyInstance(); System.out.println("NumberFormat.getCurrencyInstance()"); System.out.println("Currency " + c + " is displayed as " + nfCurrency.format(c)); // 这里没有涉及相应的parse方法 } public static void customNumberFormat() { double x = 1000.0 / 3; System.out.println("default output is " + x);//333.3333333333333 patternPrint("###,###.##", x);//333.33 patternPrint("####.##", x);//333.33 patternPrint("####.00", x);//333.33 patternPrint("####.0#", x);//333.33 patternPrint("00000.##", x);//00333.33 patternPrint("$###,###.##", x);//$333.33 patternPrint("0.###E0", x);//3.333E2 patternPrint("00.##%", x);//33333.33% double y = 23.0012; System.out.println("default output is " + y);//23.0012 patternPrint("###,###.##", y);//23 patternPrint("####.##", y);//23 patternPrint("####.00", y);//23.00 patternPrint("####.0#", y);//23.0 patternPrint("00000.##", y);//00023 patternPrint("$###,###.##", y);//$23 patternPrint("0.###E0", y);//2.3E1 patternPrint("00.##%", y);//2300.12% } public static void patternPrint(String pattern, double x) { DecimalFormat df = new DecimalFormat(pattern); System.out.println("output for pattern " + pattern + " is " + df.format(x)); } public static void defaultDateFormat() { Date date = new Date(); System.out.println("simple date " + date.toLocaleString());//2007-8-31 14:40:24 DateFormat df = DateFormat.getDateTimeInstance(); System.out.println(df.format(date));//2007-8-31 14:40:24 DateFormat dfLong = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); System.out.println(dfLong.format(date));//2007年8月31日 下午02时40分24秒 DateFormat dfMedium = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); System.out.println(dfMedium.format(date));//2007-8-31 14:40:24 DateFormat dfShort = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); System.out.println(dfShort.format(date));//07-8-31 下午2:40 } public static void customDateFormat() { Date date = new Date(); patternPrint("yyyy.MM.dd HH:mm:ss z", date); // 两个MM, dd会导致补零//2007.08.31 14:40:24 CST patternPrint("yy年M月d日 HH时mm分", date); // 两个yy就显示为两位年份//07年8月31日 14时40分 patternPrint("EEE, MMM d, ''yy", date);//星期五, 八月 31, '07 patternPrint("h:mm a", date);//2:40 下午 patternPrint("hh 'o''clock' a, zzzz", date);//02 o'clock 下午, 中国标准时间 patternPrint("yyyyy.MMMMM.dd GGG hh:mm aaa", date);//02007.八月.31 公元 02:40 下午 patternPrint("EEE, d MMM yyyy HH:mm:ss Z", date);//星期五, 31 八月 2007 14:40:24 +0800 patternPrint("yyMMddHHmmssZ", date);//070831144024+0800 } public static void patternPrint(String pattern, Date date) { SimpleDateFormat df = new SimpleDateFormat(pattern); System.out.println(df.format(date)); } } 输出: NumberFormat.getInstance() Integer 123456 is displayed as 123,456 Double 882323.23523 is displayed as 882,323.235 NumberFormat.getIntegerInstance() Integer 123456 is displayed as 123,456 NumberFormat.getNumberInstance() Double 882323.23523 is displayed as 882,323.235 NumberFormat.getPercentInstance() Percent 0.528 is displayed as 53% NumberFormat.getCurrencyInstance() Currency 0.528 is displayed as ¥52.83 default output is 333.3333333333333 output for pattern ###,###.## is 333.33 output for pattern ####.## is 333.33 output for pattern ####.00 is 333.33 output for pattern ####.0# is 333.33 output for pattern 00000.## is 00333.33 output for pattern $###,###.## is $333.33 output for pattern 0.###E0 is 3.333E2 output for pattern 00.##% is 33333.33% default output is 23.0012 output for pattern ###,###.## is 23 output for pattern ####.## is 23 output for pattern ####.00 is 23.00 output for pattern ####.0# is 23.0 output for pattern 00000.## is 00023 output for pattern $###,###.## is $23 output for pattern 0.###E0 is 2.3E1 output for pattern 00.##% is 2300.12% simple date 2007-8-31 14:40:24 2007-8-31 14:40:24 2007年8月31日 下午02时40分24秒 2007-8-31 14:40:24 07-8-31 下午2:40 2007.08.31 14:40:24 CST 07年8月31日 14时40分 星期五, 八月 31, '07 2:40 下午 02 o'clock 下午, 中国标准时间 02007.八月.31 公元 02:40 下午 星期五, 31 八月 2007 14:40:24 +0800 070831144024+0800