網上轉載java
開始使用new Date()測試,並用經過date.getMonth(),和date.getDay()獲取,不事後來發現這兩個訪求是jdk1.1版本的,如今已經不用了,並且結果也不正確.web
獲取當前的月份和日期測試
試了一下,果真正確spa
後來查看java doc文檔,MONTH字段解釋以下code
Field number for get
and set
indicating the month. This is a calendar-specific value. The first month of the year is JANUARY
which is 0; the last depends on the number of months in a year.orm
這個字段的值只是說明get()的屬性字段值,來獲取month的ci
如下爲獲取其它:文檔
Calendar CD = Calendar.getInstance(); int YY = CD.get(Calendar.YEAR) ; int MM = CD.get(Calendar.MONTH)+1; int DD = CD.get(Calendar.DATE); int HH = CD.get(Calendar.HOUR); int NN = CD.get(Calendar.MINUTE); int SS = CD.get(Calendar.SECOND); int MI = CD.get(Calendar.MILLISECOND);get
Calendar cal = Calendar.getInstance();it
//當前年 int year = cal.get(Calendar.YEAR);
//當前月 int month = (cal.get(Calendar.MONTH))+1;
//當前月的第幾天:即當前日 int day_of_month = cal.get(Calendar.DAY_OF_MONTH);
//當前時:HOUR_OF_DAY-24小時制;HOUR-12小時制 int hour = cal.get(Calendar.HOUR_OF_DAY);
//當前分 int minute = cal.get(Calendar.MINUTE);
//當前秒 int second = cal.get(Calendar.SECOND);
//0-上午;1-下午 int ampm = cal.get(Calendar.AM_PM);
//當前年的第幾周 int week_of_year = cal.get(Calendar.WEEK_OF_YEAR);
//當前月的第幾周 int week_of_month = cal.get(Calendar.WEEK_OF_MONTH);
//當前年的第幾天 int day_of_year = cal.get(Calendar.DAY_OF_YEAR);
WEEK_OF_MONTH 某月第幾周,嚴格以星期的起止算。好比這個月3號纔是週一,那3號纔算第1周,2號不算。以星期爲標準 DAY_OF_WEEK_IN_MONTH 某月中第幾周,按這個月1號算,1號起就是第1周,8號起就是第2周。以月份天數爲標準。
int am = calendar.get(Calendar.AM);
int pm = calendar.get(Calendar.PM); //這2行是誤用,AM和PM是做爲值的常量
正確用法
int r = calendar.get(Calendar.AM_PM);//詢上午仍是下午
if(r==Calendar.AM)//若是上午
if(r==Calendar.PM)//若是下午