Java Calendar使用

來源:互聯網
上載者:User

標籤:使用   cond   abstract   import   std   擷取   tween   最大   nim   

剛剛在敲代碼,需要擷取當前的時間,來總結下Java Calendar的使用:

The java.util.calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week.Following are the important points about Calendar

 

API 文檔上給的官方解釋:

java.util.Calendar類是一個抽象類別,在某一特定的瞬間或日曆上,提供年、月、日、小時之間的轉換提供方法。

只要來眼界下Calendar中的常量(filed)的作用

Calendar cal = Calendar.getInstance(); cal.get(Calendar.DATE);//-----------------------當天 1-31 cal.get(Calendar.DAY_OF_MONTH);//---------------當天 1-31cal.get(Calendar.DAY_OF_WEEK);//----------------從星期天開始計算,如果今天星期二,那麼返回3 cal.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與mooth是需要注意,week需要制定setFirstDayOfWeek,然而,月份則需要加減1

3.賦值時,一般採用年、月、日、時、分、秒

  Calendar.YEAR 、Calendar.MONTH 、Calendar.DAY_OF_MONTH、 Calendar.HOUR_OF_DAY 、Calendar.MINUTE、 Calendar.SECON

 

 

主要賦值:

 

                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());

 

 

// 當天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;    }

 

Java Calendar使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.