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

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

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

    隨筆-200  評(píng)論-148  文章-15  trackbacks-0

    有兩種方法:

    方法一:用java.util.Date類來實(shí)現(xiàn),并結(jié)合java.text.DateFormat類來實(shí)現(xiàn)時(shí)間的格式化,看下面代碼:

    import java.util.*; 
    import java.text.*;
    //以下默認(rèn)時(shí)間日期顯示方式都是漢語語言方式
    //一般語言就默認(rèn)漢語就可以了,時(shí)間日期的格式默認(rèn)為MEDIUM風(fēng)格,比如:2008-6-16 20:54:53
    //以下顯示的日期時(shí)間都是再Date類的基礎(chǔ)上的來的,還可以利用Calendar類來實(shí)現(xiàn)見類TestDate2.java
    public class TestDate { 
       public static void main(String[] args) { 
          Date now = new Date(); 
          Calendar cal = Calendar.getInstance(); 
          
          DateFormat d1 = DateFormat.getDateInstance(); //默認(rèn)語言(漢語)下的默認(rèn)風(fēng)格(MEDIUM風(fēng)格,比如: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風(fēng)格顯示日期和時(shí)間
          String str4 = d4.format(now);

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

     


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

    }

    運(yùn)行結(jié)果:

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

     

    方法二:用java.util.Calendar類來實(shí)現(xiàn),看下面:

    import java.util.*; 
    import java.text.*;
    //以下是利用Calendar類來實(shí)現(xiàn)日期時(shí)間的,和Date類相比較比較簡(jiǎn)單

    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);//小時(shí) 
          int second=ca.get(Calendar.SECOND);//秒
          int WeekOfYear = ca.get(Calendar.DAY_OF_WEEK); 
          
          
          System.out.println("用Calendar.getInstance().getTime()方式顯示時(shí)間: " + ca.getTime());
          System.out.println("用Calendar獲得日期是:" + year +"年"+ month +"月"+ day + "日");
          
          System.out.println("用Calendar獲得時(shí)間是:" + hour +"時(shí)"+ minute +"分"+ second +"秒");
          System.out.println(WeekOfYear);//顯示今天是一周的第幾天(我做的這個(gè)例子正好是周二,故結(jié)果顯示2,如果你再周6運(yùn)行,那么顯示6)
          
       }

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


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

     

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

     

    方法三:

    SimpleDateFormat 24小時(shí)制時(shí)間顯示

    關(guān)鍵字: java基礎(chǔ)

     

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

    -0800

     


    它有個(gè)優(yōu)點(diǎn)就是支持兩位月份,兩位日期。帶前綴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 無聲 閱讀(40393) 評(píng)論(0)  編輯  收藏 所屬分類: 職場(chǎng)生活
    主站蜘蛛池模板: 亚洲国产天堂久久久久久| 国产成人高清亚洲| 在线观看人成视频免费| 国产真实伦在线视频免费观看| 巨胸喷奶水视频www免费视频| 中文在线免费看视频| 午夜网站在线观看免费完整高清观看| 每天更新的免费av片在线观看 | 最近中文字幕大全免费版在线| 免费看又黄又无码的网站| 欧美三级在线电影免费| 精品成在人线AV无码免费看| 一级做a爱过程免费视| 亚洲妇熟XXXX妇色黄| 国产一区二区三区在线免费观看 | 波多野结衣免费视频观看| 美丽姑娘免费观看在线观看中文版| 亚洲香蕉在线观看| 狠狠亚洲狠狠欧洲2019| 成人黄动漫画免费网站视频| 永久免费观看黄网站| 亚洲自偷自偷在线成人网站传媒| 亚洲综合AV在线在线播放| 处破痛哭A√18成年片免费| 久久免费区一区二区三波多野| 国产精品亚洲天堂| 亚洲激情校园春色| 亚洲视频中文字幕| 亚洲综合精品香蕉久久网| 国产美女无遮挡免费视频| 好男人www免费高清视频在线| 久久大香香蕉国产免费网站 | 免费国产综合视频在线看| 91青青青国产在观免费影视| 日韩在线观看视频免费| 亚洲AV综合色区无码一二三区| 亚洲成aⅴ人在线观看| 在线观看亚洲人成网站| 久久久国产精品亚洲一区| 亚洲avav天堂av在线不卡| 亚洲xxxx视频|