隨筆-204  評(píng)論-90  文章-8  trackbacks-0

          首先對(duì)SimpleDateFormat有所了解,以下摘自java-doc中

          Letter??Date?or?Time?Component??Presentation??Examples??
          G??Era?designator??Text??AD??
          y??Year??Year??
          1996 ;? 96 ??
          M??Month?in?year??Month??July;?Jul;?
          07 ??
          w??Week?in?year??Number??
          27 ??
          W??Week?in?month??Number??
          2 ??
          D??Day?in?year??Number??
          189 ??
          d??Day?in?month??Number??
          10 ??
          F??Day?of?week?in?month??Number??
          2 ??
          E??Day?in?week??Text??Tuesday;?Tue??
          a??Am
          / pm?marker??Text??PM??
          H??Hour?in?day?(
          0 - 23 )??Number?? 0 ??
          k??Hour?in?day?(
          1 - 24 )??Number?? 24 ??
          K??Hour?in?am
          / pm?( 0 - 11 )??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
          - 08 : 00 ??
          Z??Time?zone??RFC?
          822 ?time?zone?? - 0800 ??


          對(duì)中國(guó)人來(lái)說(shuō),普遍使用的是"yyyy-MM-dd"。

          貼出代碼:

          import ?java.util. * ;
          import ?java.text. * ;
          public ? class ?weekDay?
          {
          ?
          public ? static ? void ?main(String[]?args)?
          ?
          {
          ??
          // ?----------------------
          ??
          // 實(shí)現(xiàn)給定某日期,判斷是星期幾。
          ??
          // ------------------------
          ??SimpleDateFormat?formatYMD? = ? new ?SimpleDateFormat( " yyyy-MM-dd " );
          ??
          // formatYMD表示的是yyyy-MM-dd格式
          ??SimpleDateFormat?formatD? = ? new ?SimpleDateFormat( " E " );
          ??
          // "E"表示"day?in?week"
          ??Date?d? = ? null ;
          ??
          try
          ??
          {
          ???d?
          = ?formatYMD.parse( " 2005-11-8 " );
          ???
          // 將String?轉(zhuǎn)換為符合格式的日期
          ??}

          ??
          catch (Exception?e)
          ??
          {
          ???e.printStackTrace();
          ??}

          ??System.out.println(formatD.format(d));
          ??
          // 將日期中的day?of?week打印
          ??
          ??
          ??
          ??
          // ---------------------------
          ??
          // 測(cè)試一下一些想法,跟主題無(wú)關(guān)
          ??
          // Date在java.util中Date類(lèi)
          ??
          // 原來(lái)java中1月用0代表,弄的測(cè)了半天
          ??
          // ---------------------------
          ??Date?testDate? = ? new ?Date();
          ??SimpleDateFormat?format1?
          = ? new ?SimpleDateFormat( " yyyy-MM-dd " );
          ??System.out.println(testDate);
          ??System.out.println(format1.format(testDate));
          ??System.out.println(testDate.getMonth());
          ??
          ??Calendar?cal?
          = ?Calendar.getInstance();
          ??cal.get(Calendar.MONTH);
          ??
          ?}


          }



          posted on 2007-01-23 15:15 一凡 閱讀(2734) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): JAVA 基礎(chǔ)
          主站蜘蛛池模板: 揭东县| 绥江县| 莱芜市| 廉江市| 灵山县| 溆浦县| 永吉县| 正镶白旗| 丰顺县| 九江市| 日土县| 和静县| 邯郸县| 邵东县| 织金县| 汉阴县| 成安县| 兴文县| 成武县| 齐河县| 黑河市| 和林格尔县| 新田县| 响水县| 日喀则市| 徐闻县| 大厂| 招远市| 博客| 嘉祥县| 加查县| 通河县| 江北区| 广平县| 普陀区| 宜章县| 东兴市| 焉耆| 林口县| 许昌市| 冕宁县|