丄諦啲仇魜ヤ
          如 果 敵 人 讓 你 生 氣 , 那 說 明 你 沒 有 勝 他 的 把 握!
          posts - 6,comments - 56,trackbacks - 1

          public class SimpleDateFormat extends DateFormat
          SimpleDateFormat 是一個(gè)以國別敏感的方式格式化和分析數(shù)據(jù)的具體類。 它允許格式化 (date -> text)、語法分析 (text -> date)和標(biāo)準(zhǔn)化。

          SimpleDateFormat 允許以為日期-時(shí)間格式化選擇任何用戶指定的方式啟動(dòng)。 但是,希望用 DateFormat 中的 getTimeInstance、 getDateInstance 或 getDateTimeInstance 創(chuàng)建一個(gè)日期-時(shí)間格式化程序。 每個(gè)類方法返回一個(gè)以缺省格式化方式初始化的日期/時(shí)間格式化程序。 可以根據(jù)需要用 applyPattern 方法修改格式化方式。

          SimpleDateFormat函數(shù)的繼承關(guān)系:
          java.lang.Object
             |
             +----java.text.Format
                     |
                     +----java.text.DateFormat
                             |
                             +----java.text.SimpleDateFormat
          下面是個(gè)小例子:
          import java.text.*;
          import java.util.Date;

          /**
            SimpleDateFormat函數(shù)語法:
           
            G 年代標(biāo)志符
            y 年
            M 月
            d 日
            h 時(shí) 在上午或下午 (1~12)
            H 時(shí) 在一天中 (0~23)
            m 分
            s 秒
            S 毫秒
            E 星期
            D 一年中的第幾天
            F 一月中第幾個(gè)星期幾
            w 一年中第幾個(gè)星期
            W 一月中第幾個(gè)星期
            a 上午 / 下午 標(biāo)記符
            k 時(shí) 在一天中 (1~24)
            K 時(shí) 在上午或下午 (0~11)
            z 時(shí)區(qū)
           */
          public class FormatDateTime {

              public static void main(String[] args) {
                  SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH時(shí)mm分ss秒");
                  SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm");
                  SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//等價(jià)于now.toLocaleString()
                  SimpleDateFormat myFmt3=new SimpleDateFormat("yyyy年MM月dd日 HH時(shí)mm分ss秒 E ");
                  SimpleDateFormat myFmt4=new SimpleDateFormat(
                          "一年中的第 D 天 一年中第w個(gè)星期 一月中第W個(gè)星期 在一天中k時(shí) z時(shí)區(qū)");
                  Date now=new Date();
                  System.out.println(myFmt.format(now));
                  System.out.println(myFmt1.format(now));
                  System.out.println(myFmt2.format(now));
                  System.out.println(myFmt3.format(now));
                  System.out.println(myFmt4.format(now));
                  System.out.println(now.toGMTString());
                  System.out.println(now.toLocaleString());
                  System.out.println(now.toString());
              }   
             
          }

          效果:
          2007年10月16日 17時(shí)24分27秒
          07/10/16 17:24
          200-10-16 17:24:27
          2007年10月16日 17時(shí)24分27秒 星期四
          一年中的第 351 天 一年中第51個(gè)星期 一月中第3個(gè)星期 在一天中17時(shí) CST時(shí)區(qū)
          16 Dec 2007 09:24:27 GMT
          2007-10-16 17:24:27
          Thu Dec 16 17:24:27 CST 200

          下面是個(gè)JavaBean:
          public class FormatDateTime {
             
              public static String toLongDateString(Date dt){
                  SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH時(shí)mm分ss秒 E ");       
                  return myFmt.format(dt);
              }
             
              public static String toShortDateString(Date dt){
                  SimpleDateFormat myFmt=new SimpleDateFormat("yy年MM月dd日 HH時(shí)mm分");       
                  return myFmt.format(dt);
              }   
             
              public static String toLongTimeString(Date dt){
                  SimpleDateFormat myFmt=new SimpleDateFormat("HH mm ss SSSS");       
                  return myFmt.format(dt);
              }
              public static String toShortTimeString(Date dt){
                  SimpleDateFormat myFmt=new SimpleDateFormat("yy/MM/dd HH:mm");       
                  return myFmt.format(dt);
              }
             
              public static void main(String[] args) {

                  Date now=new Date();

                  System.out.println(FormatDateTime.toLongDateString(now));
                  System.out.println(FormatDateTime.toShortDateString(now));
                  System.out.println(FormatDateTime.toLongTimeString(now));
                  System.out.println(FormatDateTime.toShortTimeString(now));
              }   
             
          }
          調(diào)用的main 測(cè)試結(jié)果:
          2007年12月16日 17時(shí)38分26秒 星期四
          07年12月16日 17時(shí)38分
          17 38 26 0965
          07/12/16 17:38

          posted on 2007-11-09 09:16 Crying 閱讀(366) 評(píng)論(1)  編輯  收藏 所屬分類: JAVA基礎(chǔ)

          FeedBack:
          # re: SimpleDateFormat詳解
          2008-07-29 16:07 | Crying
           
          【轉(zhuǎn)自www.bitsCN.com】
          import java.util.*;
            import java.text.*;
            public class FormatDate {
             public static void main(String[] args) {
              Date now = new Date();
            
              DateFormat defaultFormat = DateFormat.getDateInstance();
              DateFormat shortFormat = DateFormat.getDateInstance(DateFormat.SHORT);
              DateFormat mediumFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
              DateFormat longFormat = DateFormat.getDateInstance(DateFormat.LONG);
              DateFormat fullFormat = DateFormat.getDateInstance(DateFormat.FULL);
              String defaultDate = defaultFormat.format(now);
              String shortDate = shortFormat.format(now);
              String mediumDate = mediumFormat.format(now);
              String longDate = longFormat.format(now);
              String fullDate = fullFormat.format(now);
            
              System.out.println("(Default) Today :" + defaultDate);
              System.out.println("(SHORT) Today : " + shortDate);
              System.out.println("(MEDIUM) Today :" + mediumDate);

              System.out.println("(LONG) Today : " + longDate);
              System.out.println("(FULL) Today : " + fullDate);
             }
            }
            
            運(yùn)行結(jié)果為:
            D:\javamail>java FormatDate
            (Default) Today :2003-6-15
            (SHORT) Today : 03-6-15
            (MEDIUM) Today :2003-6-15
            (LONG) Today : 2003年6月15日
            (FULL) Today : 2003年6月15日 星期日
             回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 新野县| 龙门县| 汉中市| 桓仁| 焦作市| 凤台县| 道孚县| 海南省| 南宁市| 封开县| 虹口区| 河北省| 石阡县| 上饶市| 正镶白旗| 山阳县| 西乡县| 泰兴市| 金塔县| 松阳县| 清涧县| 克东县| 岗巴县| 黑山县| 灌阳县| 拉孜县| 林口县| 临安市| 清丰县| 平果县| 梓潼县| 丹凤县| 嫩江县| 靖安县| 互助| 灵台县| 武山县| 新河县| 大安市| 庆安县| 平罗县|