seaairland

           

          使用java.text包格式化數(shù)字和日期

          在C中可以使用類(lèi)似printf(“%d %8.2f\n”, 1001, 52.335)的方法實(shí)現(xiàn)格式化輸出,可是Java中的System.out.println()并沒(méi)有對(duì)應(yīng)的功能。要格式化輸出,必須使用java.text包中的類(lèi)來(lái)實(shí)現(xiàn)類(lèi)似的操作(要不怎么體現(xiàn)面向?qū)ο蟮膬?yōu)越性呢,不過(guò)據(jù)說(shuō)jdk1.5準(zhǔn)備又補(bǔ)上)。當(dāng)然了,java.text包的功能還是很強(qiáng)大的,奇怪的是很多書(shū)中都沒(méi)有涉及,而一般誰(shuí)又有工夫整天去看API?

          注意:由于這里說(shuō)得很簡(jiǎn)略,因此請(qǐng)參照下面的Demo程序。?

          格式化數(shù)字

          在NumberFormat類(lèi)中為我們提供了格式化4種數(shù)字的方法:整數(shù)、小數(shù)、貨幣和百分比,通過(guò)工廠(chǎng)方法getNumberInstance, getNumberIntance, getCurrencyInstance, getPercentInstance方法獲得相應(yīng)的實(shí)例對(duì)象就行。例如我們要以字符串表示人民幣88888.88元,這樣來(lái)寫(xiě)就行:

          NumberFormat nf = NumberFormat.getCurrencyInstance();

          System.out.println(nf.format(88888.88));

          定制格式化數(shù)字

          可是對(duì)于稍微復(fù)雜一點(diǎn)的需求,NumberFormat就滿(mǎn)足不了了,幸好java還提供了DecimalFormat實(shí)現(xiàn)定制的格式化。要使用DecimalFormat對(duì)象,必須提供給它提供一個(gè)格式化的模式(pattern):

          String pattern = …

          DecimalFormat df = new DecimalFormat(pattern);

          或者:

          DecimalFormat df = new DecimalFormat();

          df. applyPattern(pattern);

          然后就調(diào)用它的format方法就行了。

          所以關(guān)鍵就是這個(gè)模式怎么定義。在DecimalFormat類(lèi)的JavaDoc中有模式的語(yǔ)法表示,不過(guò)很難說(shuō)清楚(是我說(shuō)不清楚,呵呵),請(qǐng)看看Demo自己多試試吧。下面是模式中某些字符的含義表:

          字符???????? ?含義
          ?
          0???????????? ?一位數(shù)字,這一位缺失顯示為0。用來(lái)補(bǔ)零
          ?
          #??????????? ?一位數(shù)字, 這一位缺失就不顯示
          ?
          .?????????????? ?小數(shù)點(diǎn),不用多說(shuō)了吧
          ?
          ,?????????????? ?千位分隔符
          ?
          E?????????????? ?科學(xué)計(jì)數(shù)法
          ?
          %????????????? ?百分比
          ?

          格式化日期

          把日期轉(zhuǎn)化為字符串最簡(jiǎn)單的方法就是調(diào)用Date類(lèi)的toString或者toLocaleString方法:

          System.out.println(new Date());???

          輸出:2004-8-7 8:16:14。可是如果我們想把月和日補(bǔ)成2位不要時(shí)分秒2004-08-07,這就不靈了。java.text.DateFormat提供了大量的工廠(chǎng)方法:getDateInstance(int style), getTimeInstance(int style), getDateTimeInstance(int dateStyle, int timeStyle)等等。其中style必須是DateFormat.LONG, DateFormat.MEDIUM, DateFormat.SHORT之一。Demo中的defaultDateFormat方法作了一個(gè)簡(jiǎn)單的實(shí)驗(yàn)。

          定制格式化日期:

          同樣,java.text.SimpleDateFormat可以通過(guò)模式(pattern)實(shí)現(xiàn)定制格式化:

          String pattern = …

          SimpleDateFormat df = new SimpleDateFormat(pattern);

          或者:

          SimpleDateFormat df = new SimpleDateFormat();

          df. applyPattern(pattern);

          下面是SimpleDateFormat的javadoc中的一個(gè)模式符號(hào)簡(jiǎn)表:

          符號(hào)????? 意義??? ?????????????????????? 合法數(shù)值???? 示例
          ?
          y????????? Year??????????????????????????? Year????????? 1996; 96
          ?
          M?????? Month in year??????????????? Month???????? July; Jul; 07
          ?
          d??????? ?Day in month????????????? ?Number???? ?10
          ?
          a??????? ?Am/pm marker??????????? ?Text??????????? ?PM
          ?
          H??????? ?Hour in day (0-23)????? ?Number?????? ?0
          ?
          h??????? ?Hour in am/pm (1-12)? ?Number????? ?12
          ?
          m?????? ?Minute in hour???????????? ?Number?????? ?30
          ?
          s??????? ?Second in minute???????? ?Number????? ?55
          ?
          S????????? ?Millisecond??????????????? ?Number?????? ?978
          ?
          z??????? ?Time zone????????? ?General time zone??? ?Pacific Standard Time; PST; GMT-8:00
          ?
          Z???????? ?Time zone????????? ?RFC 822 time zone??-0800
          ?

          注意的是,符號(hào)的大小寫(xiě)意義是不同的,符號(hào)的個(gè)數(shù)也會(huì)導(dǎo)致輸出不一樣。例如用MM就會(huì)把1月份顯示成01,而用M則不會(huì)補(bǔ)零。對(duì)于年份,兩個(gè)yy會(huì)只輸出兩位年份,yyyy則會(huì)輸出4位年份。

          ?實(shí)際上,上面的類(lèi)還提供了很多其他方法,特別是用于本地化(Locale)定制格式化的方法,以及從字符串表示轉(zhuǎn)化為相應(yīng)對(duì)象的parse方法,還有把格式化結(jié)果附加到一個(gè)StringBuffer的方法(應(yīng)該是用來(lái)提高性能)。

          ?最后是一個(gè)小Demo和輸出結(jié)果:

          TestFormat.java:

          import java.text.*;

          import java.util.*;

          ?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("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("Integer " + i + " is displayed as " + nfInt.format(i));

          ?????? NumberFormat nfNumber = NumberFormat.getNumberInstance();

          ?????? System.out.println("Double " + x + " is displayed as " + nfNumber.format(x));

          ?????? NumberFormat nfPercent = NumberFormat.getPercentInstance();

          ?????? System.out.println("Percent " + p + " is displayed as " + nfPercent.format(p));

          ?????? NumberFormat nfCurrency = NumberFormat.getCurrencyInstance();

          ?????? System.out.println("Currency " + p + " is displayed as " + nfCurrency.format(c));??????

          ?????? //這里沒(méi)有涉及相應(yīng)的parse方法

          ??? }

          ??? public static void customNumberFormat() {

          ?????? double x = 1000.0 / 3;

          ?????? System.out.println("default output is " + x);

          ?????? patternPrint("###,###.##", x);

          ?????? patternPrint("####.##", x);

          ?????? patternPrint("####.00", x);

          ?????? patternPrint("####.0#", x);

          ?????? patternPrint("00000.##", x);

          ?????? patternPrint("$###,###.##", x);

          ??????? patternPrint("0.###E0", x);

          ?????? patternPrint("00.##%", x);

          ?????? double y = 23.0012;

          ?????? System.out.println("default output is " + y);

          ?????? patternPrint("###,###.##", y);

          ?????? patternPrint("####.##", y);

          ?????? patternPrint("####.00", y);

          ?????? patternPrint("####.0#", y);

          ?????? patternPrint("00000.##", y);

          ?????? patternPrint("$###,###.##", y);

          ?????? patternPrint("0.###E0", y);

          ?????? patternPrint("00.##%", y);

          ??? }

          ??? 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());

          ?????? DateFormat df = DateFormat.getDateTimeInstance();

          ?????? System.out.println(df.format(date));

          ?????? DateFormat dfLong = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);

          ?????? System.out.println(dfLong.format(date));

          ?????? DateFormat dfMedium = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,

          ?????????? DateFormat.MEDIUM);

          ?????? System.out.println(dfMedium.format(date));

          ?????? DateFormat dfShort = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);

          ?????? System.out.println(dfShort.format(date));

          ??? }

          ??? public static void customDateFormat() {

          ?????? Date date = new Date();

          ?????? patternPrint("yyyy.MM.dd HH:mm:ss z", date);? //兩個(gè)MM, dd會(huì)導(dǎo)致補(bǔ)零

          ?????? patternPrint("yy年M月d日 HH時(shí)mm分", date);? //兩個(gè)yy就顯示為兩位年份

          ?????? patternPrint("EEE, MMM d, ''yy", date);

          ?????? patternPrint("h:mm a", date);

          ?????? patternPrint("hh 'o''clock' a, zzzz", date);

          ?????? patternPrint("yyyyy.MMMMM.dd GGG hh:mm aaa", date);

          ?????? patternPrint("EEE, d MMM yyyy HH:mm:ss Z", date);

          ?????? patternPrint("yyMMddHHmmssZ", date);

          ??? }

          ??? public static void patternPrint(String pattern, Date date){

          ?????? SimpleDateFormat df = new SimpleDateFormat(pattern);

          ?????? System.out.println(df.format(date));

          ??? }

          }

          輸出:

          Integer 123456 is displayed as 123,456

          Double 882323.23523 is displayed as 882,323.235

          Integer 123456 is displayed as 123,456

          Double 882323.23523 is displayed as 882,323.235

          Percent 0.528 is displayed as 53%

          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 2004-8-7 8:16:14

          2004-8-7 8:16:14

          2004年8月7日 上午08時(shí)16分14秒

          2004-8-7 8:16:14

          04-8-7 上午8:16

          ?

          2004.08.07 08:16:14 GMT+08:00

          04年8月7日 08時(shí)16分

          星期六, 八月 7, '04

          8:16 上午

          08 o'clock 上午, GMT+08:00

          02004.八月.07 公元 08:16 上午

          星期六, 7 八月 2004 08:16:14 +0800

          040807081614+0800

          posted on 2006-05-02 14:12 chenhui 閱讀(418) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(1)

          隨筆分類(lèi)

          隨筆檔案

          文章分類(lèi)

          文章檔案

          介紹 IOC

          友情鏈接

          最新隨筆

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 若尔盖县| 庄浪县| 孝昌县| 湟中县| 资兴市| 繁峙县| 汪清县| 五华县| 绵竹市| 蒲江县| 万山特区| 麟游县| 长治县| 兴化市| 肥东县| 蒙城县| 鄂尔多斯市| 吉林省| 江孜县| 遂宁市| 常山县| 皮山县| 乐安县| 离岛区| 东明县| 耿马| 高邮市| 三河市| 曲松县| 讷河市| 常德市| 英超| 麻城市| 龙陵县| 体育| 精河县| 丰镇市| 大庆市| 新乐市| 洪泽县| 长丰县|