隨筆-204  評論-90  文章-8  trackbacks-0

          首先對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 ??


          對中國人來說,普遍使用的是"yyyy-MM-dd"。

          貼出代碼:

          import ?java.util. * ;
          import ?java.text. * ;
          public ? class ?weekDay?
          {
          ?
          public ? static ? void ?main(String[]?args)?
          ?
          {
          ??
          // ?----------------------
          ??
          // 實現(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打印
          ??
          ??
          ??
          ??
          // ---------------------------
          ??
          // 測試一下一些想法,跟主題無關(guān)
          ??
          // Date在java.util中Date類
          ??
          // 原來java中1月用0代表,弄的測了半天
          ??
          // ---------------------------
          ??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 一凡 閱讀(2733) 評論(0)  編輯  收藏 所屬分類: JAVA 基礎(chǔ)
          主站蜘蛛池模板: 鄂温| 常州市| 新野县| 建昌县| 周宁县| 乌苏市| 乐平市| 澄迈县| 河津市| 贵阳市| 张家港市| 陆丰市| 仪征市| 于都县| 万宁市| 文山县| 乌鲁木齐市| 湖北省| 黔西| 余干县| 睢宁县| 新沂市| 通榆县| 迁安市| 平陆县| 大足县| 瑞安市| 鞍山市| 阿合奇县| 吉木乃县| 赤壁市| 敦化市| 桃源县| 游戏| 罗田县| 萨迦县| 清苑县| 铁岭县| 柘城县| 景德镇市| 麻江县|