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

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

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

    posts - 104,  comments - 34,  trackbacks - 0

    今天自己寫的一個用于取得當前日期相對應的月初,月末,季初,季末,年初,年末,返回值均為String字符串,方法比較土,使用字符串套接方法完成。

    package com.zrar.date;

    import java.util.Calendar;

    /**
     *
     * 描述:此類用于取得當前日期相對應的月初,月末,季初,季末,年初,年末,返回值均為String字符串
     *      1、得到當前日期         today()
     *      2、得到當前月份月初      thisMonth()
     *      3、得到當前月份月底      thisMonthEnd()
     *      4、得到當前季度季初      thisSeason()
     *      5、得到當前季度季末      thisSeasonEnd()
     *      6、得到當前年份年初      thisYear()
     *      7、得到當前年份年底      thisYearEnd()
     *      8、判斷輸入年份是否為閏年 leapYear
     *     
     * 注意事項:  日期格式為:xxxx-yy-zz (eg: 2007-12-05)
     *
     * 實例:
     *
     * @author pure
     */
    public class DateThis {

        private int x;                  // 日期屬性:年

        private int y;                  // 日期屬性:月

        private int z;                  // 日期屬性:日

        private Calendar localTime;     // 當前日期

        public DateThis() {
            localTime = Calendar.getInstance();
        }

        /**
         * 功能:得到當前日期 格式為:xxxx-yy-zz (eg: 2007-12-05)<br>
         * @return String
         * @author pure
         */
        public String today() {
            String strY = null;
            String strZ = null;
            x = localTime.get(Calendar.YEAR);
            y = localTime.get(Calendar.MONTH) + 1;
            z = localTime.get(Calendar.DATE);
            strY = y >= 10 ? String.valueOf(y) : ("0" + y);
            strZ = z >= 10 ? String.valueOf(z) : ("0" + z);
            return x + "-" + strY + "-" + strZ;
        }

        /**
         * 功能:得到當前月份月初 格式為:xxxx-yy-zz (eg: 2007-12-01)<br>
         * @return String
         * @author pure
         */
        public String thisMonth() {
            String strY = null;
            x = localTime.get(Calendar.YEAR);
            y = localTime.get(Calendar.MONTH) + 1;
            strY = y >= 10 ? String.valueOf(y) : ("0" + y);
            return x + "-" + strY + "-01";
        }

        /**
         * 功能:得到當前月份月底 格式為:xxxx-yy-zz (eg: 2007-12-31)<br>
         * @return String
         * @author pure
         */
        public String thisMonthEnd() {
            String strY = null;
            String strZ = null;
            boolean leap = false;
            x = localTime.get(Calendar.YEAR);
            y = localTime.get(Calendar.MONTH) + 1;
            if (y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12) {
                strZ = "31";
            }
            if (y == 4 || y == 6 || y == 9 || y == 11) {
                strZ = "30";
            }
            if (y == 2) {
                leap = leapYear(x);
                if (leap) {
                    strZ = "29";
                }
                else {
                    strZ = "28";
                }
            }
            strY = y >= 10 ? String.valueOf(y) : ("0" + y);
            return x + "-" + strY + "-" + strZ;
        }

        /**
         * 功能:得到當前季度季初 格式為:xxxx-yy-zz (eg: 2007-10-01)<br>
         * @return String
         * @author pure
         */
        public String thisSeason() {
            String dateString = "";
            x = localTime.get(Calendar.YEAR);
            y = localTime.get(Calendar.MONTH) + 1;
            if (y >= 1 && y <= 3) {
                dateString = x + "-" + "01" + "-" + "01";
            }
            if (y >= 4 && y <= 6) {
                dateString = x + "-" + "04" + "-" + "01";
            }
            if (y >= 7 && y <= 9) {
                dateString = x + "-" + "07" + "-" + "01";
            }
            if (y >= 10 && y <= 12) {
                dateString = x + "-" + "10" + "-" + "01";
            }
            return dateString;
        }

        /**
         * 功能:得到當前季度季末 格式為:xxxx-yy-zz (eg: 2007-12-31)<br>
         * @return String
         * @author pure
         */
        public String thisSeasonEnd() {
            String dateString = "";
            x = localTime.get(Calendar.YEAR);
            y = localTime.get(Calendar.MONTH) + 1;
            if (y >= 1 && y <= 3) {
                dateString = x + "-" + "03" + "-" + "31";
            }
            if (y >= 4 && y <= 6) {
                dateString = x + "-" + "06" + "-" + "30";
            }
            if (y >= 7 && y <= 9) {
                dateString = x + "-" + "09" + "-" + "30";
            }
            if (y >= 10 && y <= 12) {
                dateString = x + "-" + "12" + "-" + "31";
            }
            return dateString;
        }

        /**
         * 功能:得到當前年份年初 格式為:xxxx-yy-zz (eg: 2007-01-01)<br>
         * @return String
         * @author pure
         */
        public String thisYear() {
            x = localTime.get(Calendar.YEAR);
            return x + "-01" + "-01";
        }

        /**
         * 功能:得到當前年份年底 格式為:xxxx-yy-zz (eg: 2007-12-31)<br>
         * @return String
         * @author pure
         */
        public String thisYearEnd() {
            x = localTime.get(Calendar.YEAR);
            return x + "-12" + "-31";
        }

        /**
         * 功能:判斷輸入年份是否為閏年<br>
         *
         * @param year
         * @return 是:true  否:false
         * @author pure
         */
        public boolean leapYear(int year) {
            boolean leap;
            if (year % 4 == 0) {
                if (year % 100 == 0) {
                    if (year % 400 == 0) leap = true;
                    else leap = false;
                }
                else leap = true;
            }
            else leap = false;
            return leap;
        }
    }


     

    posted on 2007-12-04 15:10 末日風情 閱讀(4527) 評論(1)  編輯  收藏

    FeedBack:
    # re: 用于取得當前日期相對應的月初,月末,季初,季末,年初,年末時間
    2008-03-17 16:02 | jdskyy
    謝謝,實用的很  回復  更多評論
      

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


    網站導航:
    博客園   IT新聞   Chat2DB   C++博客   博問  
     
    <2007年12月>
    2526272829301
    2345678
    9101112131415
    16171819202122
    23242526272829
    303112345

    常用鏈接

    留言簿(4)

    隨筆分類

    隨筆檔案

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲熟妇无码av另类vr影视| 无人视频免费观看免费视频| 黄色片在线免费观看| 亚洲一级特黄特黄的大片| 国产精品jizz在线观看免费| 国产精品视频全国免费观看| 亚洲AV无码乱码在线观看裸奔| 亚洲人成免费电影| 亚洲av无码专区在线电影| 国产精品亚洲精品日韩已方| h片在线免费观看| 日日摸日日碰夜夜爽亚洲| 国产亚洲精品岁国产微拍精品| 1000部啪啪未满十八勿入免费| 亚洲精品无码久久久久牙蜜区| 久久亚洲国产精品123区| 69视频在线观看免费| 国产亚洲精品仙踪林在线播放| 亚洲国产第一页www| 国产成人免费一区二区三区| 中文字幕乱码一区二区免费| 亚洲人成人网站18禁| 亚洲VA成无码人在线观看天堂 | 99在线观看免费视频| 亚洲人成色77777在线观看| 亚洲精品无码午夜福利中文字幕| 成年丰满熟妇午夜免费视频| 免费一级不卡毛片| 国产亚洲人成在线影院| 亚洲午夜在线电影| 亚洲男人的天堂一区二区| 国产免费毛不卡片| 日本免费中文字幕| 一级特黄a免费大片| 亚洲午夜无码久久久久软件| 亚洲第一福利网站| 亚洲福利视频一区二区| 性xxxx视频播放免费| 又黄又爽又成人免费视频| 久久久久久久久久国产精品免费 | 亚洲第一综合天堂另类专 |