1. 程式人生 > >Android Calendar獲取年月日時分秒毫秒

Android Calendar獲取年月日時分秒毫秒

開始使用new Date()測試,並用通過date.getMonth(),和date.getDay()獲取,不過後來發現這兩個訪求是jdk1.1版本的,現在已經不用了,而且結果也不正確.

int month = (date.get(Calendar.MONTH))+1;
int day = date.get(Calendar.DAY_OF_MONTH); 

獲取當前的月份和日期

試了一下,果然正確

後來檢視java doc文件,MONTH欄位解釋如下

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.

這個欄位的值只是說明get()的屬性欄位值,來獲取month的

以下為獲取其它:

複製程式碼
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); Calendar cal = Calendar.getInstance(); //當前年 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);