Energy of Love  
          日歷
          <2009年5月>
          262728293012
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456
          統(tǒng)計
          • 隨筆 - 70
          • 文章 - 0
          • 評論 - 80
          • 引用 - 0

          導(dǎo)航

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

           

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

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

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

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

               public static void main(String[] args) {
                   SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH時mm分ss秒");
                   SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm");
                   SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//等價于now.toLocaleString()
                   SimpleDateFormat myFmt3=new SimpleDateFormat("yyyy年MM月dd日 HH時mm分ss秒 E ");
                   SimpleDateFormat myFmt4=new SimpleDateFormat(
                           "一年中的第 D 天 一年中第w個星期 一月中第W個星期 在一天中k時 z時區(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());
               }   
             
          }

          效果:
          2004年12月16日 17時24分27秒
          04/12/16 17:24
          2004-12-16 17:24:27
          2004年12月16日 17時24分27秒 星期四
          一年中的第 351 天 一年中第51個星期 一月中第3個星期 在一天中17時 CST時區(qū)
          16 Dec 2004 09:24:27 GMT
          2004-12-16 17:24:27
          Thu Dec 16 17:24:27 CST 2004

          下面是個JavaBean:
          public class FormatDateTime {
             
               public static String toLongDateString(Date dt){
                   SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH時mm分ss秒 E ");       
                   return myFmt.format(dt);
               }
             
               public static String toShortDateString(Date dt){
                   SimpleDateFormat myFmt=new SimpleDateFormat("yy年MM月dd日 HH時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 測試結(jié)果:
          2004年12月16日 17時38分26秒 星期四
          04年12月16日 17時38分
          17 38 26 0965
          04/12/16 17:38

           

           


          在java中取得當(dāng)前的系統(tǒng)時間并且轉(zhuǎn)化成自己想要的格式
               需要引進(jìn) java.util.Calendar 和 java.text.SimpleDateFormat 這兩個類:

                  Calendar rightNow = Calendar.getInstance();
                   SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddhhmmss");
                   String sysDatetime = fmt.format(rightNow.getTime());  

              可以對 new SimpleDateFormat("yyyyMMddhhmmss") 中引號里面的格式進(jìn)行編輯,轉(zhuǎn)換成自己相要的格式,比如還可以轉(zhuǎn)
              換成    new    SimpleDateFormat("yyyy/MM/dd    hh:mm:ss    ") 的格式。

           
          文章出處:DIY部落(http://www.diybl.com/course/3_program/java/javajs/20090302/156356.html)

          posted on 2009-05-19 12:47 不高興 閱讀(205) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
           
          Copyright © 不高興 Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 郯城县| 南靖县| 上虞市| 项城市| 永修县| 炉霍县| 综艺| 农安县| 厦门市| 威海市| 惠州市| 邓州市| 顺昌县| 绍兴县| 清河县| 韩城市| 勐海县| 绵阳市| 海盐县| 彩票| 灵山县| 盘山县| 和政县| 南岸区| 海盐县| 甘洛县| 射阳县| 玛纳斯县| 高碑店市| 岚皋县| 宜阳县| 商洛市| 太原市| 浙江省| 衡阳县| 南宁市| 潮州市| 济源市| 南靖县| 吉安县| 靖宇县|