<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-200  評論-148  文章-15  trackbacks-0

    有兩種方法:

    方法一:用java.util.Date類來實現,并結合java.text.DateFormat類來實現時間的格式化,看下面代碼:

    import java.util.*; 
    import java.text.*;
    //以下默認時間日期顯示方式都是漢語語言方式
    //一般語言就默認漢語就可以了,時間日期的格式默認為MEDIUM風格,比如:2008-6-16 20:54:53
    //以下顯示的日期時間都是再Date類的基礎上的來的,還可以利用Calendar類來實現見類TestDate2.java
    public class TestDate { 
       public static void main(String[] args) { 
          Date now = new Date(); 
          Calendar cal = Calendar.getInstance(); 
          
          DateFormat d1 = DateFormat.getDateInstance(); //默認語言(漢語)下的默認風格(MEDIUM風格,比如:2008-6-16 20:54:53)
          String str1 = d1.format(now);
          DateFormat d2 = DateFormat.getDateTimeInstance(); 
          String str2 = d2.format(now); 
          DateFormat d3 = DateFormat.getTimeInstance(); 
          String str3 = d3.format(now); 
          DateFormat d4 = DateFormat.getInstance(); //使用SHORT風格顯示日期和時間
          String str4 = d4.format(now);

          DateFormat d5 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL); //顯示日期,周,時間(精確到秒)
          String str5 = d5.format(now);
          DateFormat d6 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); //顯示日期。時間(精確到秒)
          String str6 = d6.format(now);
          DateFormat d7 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); //顯示日期,時間(精確到分)
          String str7 = d7.format(now);
          DateFormat d8 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM); //顯示日期,時間(精確到分)
          String str8 = d8.format(now);//與SHORT風格相比,這種方式最好用

     


          
          System.out.println("用Date方式顯示時間: " + now);//此方法顯示的結果和Calendar.getInstance().getTime()一樣
          
          
          System.out.println("用DateFormat.getDateInstance()格式化時間后為:" + str1);
          System.out.println("用DateFormat.getDateTimeInstance()格式化時間后為:" + str2);
          System.out.println("用DateFormat.getTimeInstance()格式化時間后為:" + str3);
          System.out.println("用DateFormat.getInstance()格式化時間后為:" + str4);
          
          System.out.println("用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化時間后為:" + str5);
          System.out.println("用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化時間后為:" + str6);
          System.out.println("用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化時間后為:" + str7);
          System.out.println("用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化時間后為:" + str8);
       }

    }

    運行結果:

    用Date方式顯示時間: Mon Jun 16 20:54:53 CST 2008
    用DateFormat.getDateInstance()格式化時間后為:2008-6-16
    用DateFormat.getDateTimeInstance()格式化時間后為:2008-6-16 20:54:53
    用DateFormat.getTimeInstance()格式化時間后為:20:54:53
    用DateFormat.getInstance()格式化時間后為:08-6-16 下午8:54
    用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化時間后為
    :2008年6月16日 星期一 下午08時54分53秒 CST
    用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化時間后為
    :2008年6月16日 下午08時54分53秒
    用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化時間后
    為:08-6-16 下午8:54
    用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化時間
    后為:2008-6-16 20:54:53

     

    方法二:用java.util.Calendar類來實現,看下面:

    import java.util.*; 
    import java.text.*;
    //以下是利用Calendar類來實現日期時間的,和Date類相比較比較簡單

    public class TestDate2 { 
       public static void main(String[] args) { 
          
          Calendar ca = Calendar.getInstance();
          int year = ca.get(Calendar.YEAR);//獲取年份
          int month=ca.get(Calendar.MONTH);//獲取月份 
          int day=ca.get(Calendar.DATE);//獲取日
          int minute=ca.get(Calendar.MINUTE);//分 
          int hour=ca.get(Calendar.HOUR);//小時 
          int second=ca.get(Calendar.SECOND);//秒
          int WeekOfYear = ca.get(Calendar.DAY_OF_WEEK); 
          
          
          System.out.println("用Calendar.getInstance().getTime()方式顯示時間: " + ca.getTime());
          System.out.println("用Calendar獲得日期是:" + year +"年"+ month +"月"+ day + "日");
          
          System.out.println("用Calendar獲得時間是:" + hour +"時"+ minute +"分"+ second +"秒");
          System.out.println(WeekOfYear);//顯示今天是一周的第幾天(我做的這個例子正好是周二,故結果顯示2,如果你再周6運行,那么顯示6)
          
       }

    }
    運行結果是:
    用Calendar.getInstance().getTime()方式顯示時間: Mon Jun 16 21:54:21 CST 2008
    用Calendar獲得日期是:2008年5月16日
    用Calendar獲得時間是:9時54分21秒
    2


    總結:中的來說,方法二是最方便的,方法一顯得分笨拙,不過看個人喜歡了。

     

    轉自:http://student.csdn.net/space.php?uid=122120&do=blog&id=14681

     

    方法三:

    SimpleDateFormat 24小時制時間顯示

    關鍵字: java基礎

     

    字母日期或時間元素表示示例
    GEra 標志符TextAD
    yYear199696
    M年中的月份MonthJulyJul07
    w年中的周數Number27
    W月份中的周數Number2
    D年中的天數Number189
    d月份中的天數Number10
    F月份中的星期Number2
    E星期中的天數TextTuesdayTue
    aAm/pm 標記TextPM
    H一天中的小時數(0-23)Number0
    k一天中的小時數(1-24)Number24
    Kam/pm 中的小時數(0-11)Number0
    ham/pm 中的小時數(1-12)Number12
    m小時中的分鐘數Number30
    s分鐘中的秒數Number55
    S毫秒數Number978
    z時區General time zonePacific Standard TimePSTGMT-08:00
    Z時區RFC 822 time zone

    -0800

     


    它有個優點就是支持兩位月份,兩位日期。帶前綴0.
    Date date = new Date();
    SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    String sDateSuffix = dateformat.format(date);
    System.out.println("[+]sDateSuffix:"+sDateSuffix);
    posted on 2013-01-17 18:47 無聲 閱讀(40410) 評論(0)  編輯  收藏 所屬分類: 職場生活
    主站蜘蛛池模板: 免费观看黄网站在线播放| 免费va在线观看| 成年在线网站免费观看无广告| 国产无遮挡又黄又爽免费视频 | 久久综合AV免费观看| 亚洲资源在线观看| 国产亚洲精品91| 99re6在线精品视频免费播放 | 亚洲中文字幕无码不卡电影| 亚洲国产精品成人综合色在线婷婷| 精品韩国亚洲av无码不卡区| 91av免费观看| 国产亚洲精品高清在线| 亚洲av无码一区二区三区人妖| 最近中文字幕大全免费版在线| 国产大片免费观看中文字幕| 亚洲精品无码久久久久久| 一级毛片aaaaaa免费看| 亚洲国产婷婷综合在线精品| 亚洲日韩国产AV无码无码精品| 成人毛片视频免费网站观看| WWW亚洲色大成网络.COM | 日本免费无遮挡吸乳视频电影| 亚洲AV日韩AV永久无码久久| 精品在线视频免费| 国产L精品国产亚洲区久久| 久久精品国产亚洲AV未满十八| 亚洲高清最新av网站| 亚洲乱色伦图片区小说| 亚洲成a人在线看天堂无码| 久草免费福利视频| 亚洲av午夜福利精品一区| 人妻免费久久久久久久了| 免费少妇a级毛片| 免费无码作爱视频| 亚洲精品天堂在线观看| 精品成在人线AV无码免费看 | 亚洲人成未满十八禁网站| 亚洲精品岛国片在线观看| 8x网站免费入口在线观看| 亚洲精品无码国产片|