java的Calendar類

來源:互聯網
上載者:User

java的Calendar類

java中的Calendar在開發中經常被忽略,這篇部落格總結一下這個類,對後面項目中使用時期的時候有協助。
Calendar常量(field)的作用
Calendar cal = Calendar.getInstance();cal.get(Calendar.DATE);//-----------------------當天 1-31cal.get(Calendar.DAY_OF_MONTH);//---------------當天 1-31cal.get(Calendar.DAY_OF_WEEK);//----------------從星期天開始計算,如果今天星期二,那麼返回3cal.get(Calendar.DAY_OF_YEAR);//----------------cal.get(Calendar.HOUR);//-----------------------12小時制cal.get(Calendar.HOUR_OF_DAY);//----------------24小時制,一般使用這個屬性賦值cal.get(Calendar.MILLISECOND);//----------------cal.get(Calendar.MINUTE);//---------------------cal.get(Calendar.SECOND);//---------------------cal.get(Calendar.WEEK_OF_MONTH);//--------------cal.get(Calendar.WEEK_OF_YEAR);//---------------cal.get(Calendar.MONTH);//-----------------------月份擷取需要 +1,那麼,賦值時需要 -1總結:1)常量的真正意義如上,我們一般使用這些常量進行賦值,換句話說,可以通過它擷取值同樣可以通過它進行對應賦值2)賦值時,week 與 month 是很值得注意的,week 需要指定 setFirstDayOfWeek , 然而,月份則需要加減 13)賦值時,我們一般採用 年 月 日 時 分 秒 Calendar.YEAR 、Calendar.MONTH 、Calendar.DAY_OF_MONTH、 Calendar.HOUR_OF_DAY 、Calendar.MINUTE、 Calendar.SECOND 
主要指派陳述式
 cal.set(Calendar.XXX, VVVV);//--------------------- 對以上每個欄位(field)進行賦值,代碼重複較大 cal.set(year,month,date,hour,minute,second);//----- 分別對欄位(field)進行賦值,效率高
主要計算
 cal1.roll(Calendar.MONTH,3);//---------------------- 一般不使用,原因是該方法只在一個月裡面迴圈計算,其大小不會超過該月最值 cal1.add(Calendar.YEAR,-1);//----------------------- 使用 XX_OF_XX 的field進行加減計算效果更佳,而且計算準確 cal1.add(field,value);//----------------------------總結:    1)關於roll的計算,cal.roll(Calendar.DAY_OF_MONTH, 32);雖然32已經超出了最大的可能31,但是cal實際是不會超出該月的,而是把32減去該月天數之後,重新計算剩下的天數;    2)關於add的計算,cal1.add(Calendar.MONTH, 1); 如果當前為8-31,那麼,加一個月的話就是9-30,這個才是真正的準確
主要取值語句
    cal.getMaximum(Calendar.DATE);    cal.get(Calendar.DATE);    cal.getMinimum(Calendar.DATE);    cal.setTimeInMillis(cal.getTime().getTime());    cal.setTimeInMillis(new Date().getTime());總結:    1)擷取最大值,最小值是很常用的方法    2)擷取毫秒數後,可以通過 1000*60*60 進行計算
Calendar 擷取當天\當月\當周
// 當天    public String getThisToday(){            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        Calendar cal = Calendar.getInstance();        cal.set(Calendar.HOUR_OF_DAY,0);        cal.set(Calendar.MINUTE, 0);        cal.set(Calendar.SECOND,0);        String start = sdf.format(cal.getTime());        cal.set(Calendar.HOUR_OF_DAY,23);        cal.set(Calendar.MINUTE, 59);        cal.set(Calendar.SECOND,59);        String end = sdf.format(cal.getTime());        return start+"|"+end;    }
// 本周     public String getThisWeekDate(){             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");            Calendar ca = Calendar.getInstance();            ca.setFirstDayOfWeek(Calendar.MONDAY);            ca.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);    ca.set(ca.get(Calendar.YEAR), ca.get(Calendar.MONTH),ca.get(Calendar.DATE),23,59,59);            String end = sdf.format(ca.getTime());            ca.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);            ca.set(Calendar.HOUR_OF_DAY,0);            ca.set(Calendar.MINUTE, 0);            ca.set(Calendar.SECOND,0);            String start = sdf.format(ca.getTime());            return start+"|"+end;      }
//本月日期段        public String getThisMonthDate(){         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        Calendar cc2 = Calendar.getInstance();        int maxMonthDay = cc2.getActualMaximum(Calendar.DAY_OF_MONTH);        cc2.set(cc2.get(Calendar.YEAR), cc2.get(Calendar.MONTH),maxMonthDay,23,59,59);        String end = sdf.format(cc2.getTime());        cc2.set(cc2.get(Calendar.YEAR), cc2.get(Calendar.MONTH),1,0,0,0);        String start = sdf.format(cc2.getTime());        return start+"|"+end;        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.