2012-07-23

【Android】日期的存取/運算

Android 中,要對日期做運算,要用到 Calendar 這個類別,而此類別位於 java.util.Calendar,可以看到是以 java 開頭的,所以也適用於 java 程式中。

首先宣告建立 Calendar 實體

Calendar  mCal = Calendar.getInstance();

物件一生成時, 便已存有目前的日期及時間.

// 本月第一天
mCal.set(Calendar.DAY_OF_MONTH,1);

// 本月最後一天
mCal.set(Calendar.DAY_OF_MONTH, mCal.getActualMaximum(Calendar.DAY_OF_MONTH)); 

// 到前 x 個月
mCal.add(Calendar.MONTH, -x);

// 到後 x 個月
mCal.add(Calendar.MONTH, x);

// 到前 x 天
mCal.add(Calendar.DATE, -x);

// 到後 x 天
mCal.add(Calendar.DATE, x);

// 當月天數
mCal.getActualMaximum(Calendar.DAY_OF_MONTH);

// 直接設定年份為 1967
mCal.set(Calendar.YEAR, 1967);

// 直接設定月份為 2 月,注意月份是從 0 起頭
mCal.set(Calendar.MONTH, 1);

// 直接設定時間的時欄位為下午 1
mCal.set(Calendar.HOUR, 13);

// 直接設定時間的分欄位為 22 分
mCal.set(Calendar.MINUTE, 22);

// 直接設定時間的秒欄位為 33 秒
mCal.set(Calendar.SECOND, 33);

// 判斷現在為 AMPM0: AM1: PM
mCal.get(Calendar.AM_PM);

// 判斷是否為閏年,先將月份移至 2
// 再看 2 月有幾天,29 天為閏年,其他則為平年
mCal.set(Calendar.MONTH, 1);
if(mCal.getActualMaximum(Calendar.DAY_OF_MONTH)==29)
       setTitle("今年為閏年");
else
       setTitle("今年不是閏年,是平年");

// 今天是星期幾
mCal.get(Calendar.DAY_OF_WEEK);

// 查日曆日, 即今天是今年的第幾天
mCal.get(Calendar.DAY_OF_YEAR);

// 本週是今年第幾週
mCal.get(Calendar.WEEK_OF_YEAR);

// 設定每週首日為星期日
mCal.setFirstDayOfWeek(Calendar.SUNDAY);

// 設定每週首日為星期一
mCal.setFirstDayOfWeek(Calendar.MONDAY);

// 查每週首日,1: 星期日,2: 星期一
mCal.getFirstDayOfWeek();

從上面的例子可以發現, 一月整數值是 0星期日是 1,各日期單位的第一順位(起始) 的整數值都不相同,所以善用 Calendar 類別常數就不需辛苦死記了。



下列是 Calendar 類別常數, 記得使用時要以 Calendar 開頭,例如上述範例的 Calendar.SUNDAYCalendar.DAY_OF_WEEK......等  ----

int ALL_STYLES Requests both SHORT and LONG styles in the map returned by getDisplayNames(int, int, Locale).
int AM Value of the AM_PM field indicating the period of the day from midnight to just before noon.
int AM_PM Field number for get and set indicating whether the HOUR is before or after noon.
int APRIL Value of the MONTH field indicating the fourth month of the year.
int AUGUST Value of the MONTH field indicating the eighth month of the year.
int DATE Field number for get and set indicating the day of the month.
int DAY_OF_MONTH Field number for get and set indicating the day of the month.
int DAY_OF_WEEK Field number for get and set indicating the day of the week.
int DAY_OF_WEEK_IN_MONTH Field number for get and set indicating the ordinal number of the day of the week within the current month.
int DAY_OF_YEAR Field number for get and set indicating the day number within the current year.
int DECEMBER Value of the MONTH field indicating the twelfth month of the year.
int DST_OFFSET Field number for get and set indicating the daylight savings offset in milliseconds.
int ERA Field number for get and set indicating the era, e.g., AD or BC in the Julian calendar.
int FEBRUARY Value of the MONTH field indicating the second month of the year.
int FIELD_COUNT This is the total number of fields in this calendar.
int FRIDAY Value of the DAY_OF_WEEK field indicating Friday.
int HOUR Field number for get and set indicating the hour of the morning or afternoon.
int HOUR_OF_DAY Field number for get and set indicating the hour of the day.
int JANUARY Value of the MONTH field indicating the first month of the year.
int JULY Value of the MONTH field indicating the seventh month of the year.
int JUNE Value of the MONTH field indicating the sixth month of the year.
int LONG Requests long names (such as "January") from getDisplayName(int, int, Locale) or getDisplayNames(int, int, Locale).
int MARCH Value of the MONTH field indicating the third month of the year.
int MAY Value of the MONTH field indicating the fifth month of the year.
int MILLISECOND Field number for get and set indicating the millisecond within the second.
int MINUTE Field number for get and set indicating the minute within the hour.
int MONDAY Value of the DAY_OF_WEEK field indicating Monday.
int MONTH Field number for get and set indicating the month.
int NOVEMBER Value of the MONTH field indicating the eleventh month of the year.
int OCTOBER Value of the MONTH field indicating the tenth month of the year.
int PM Value of the AM_PM field indicating the period of the day from noon to just before midnight.
int SATURDAY Value of the DAY_OF_WEEK field indicating Saturday.
int SECOND Field number for get and set indicating the second within the minute.
int SEPTEMBER Value of the MONTH field indicating the ninth month of the year.
int SHORT Requests short names (such as "Jan") from getDisplayName(int, int, Locale) or getDisplayNames(int, int, Locale).
int SUNDAY Value of the DAY_OF_WEEK field indicating Sunday.
int THURSDAY Value of the DAY_OF_WEEK field indicating Thursday.
int TUESDAY Value of the DAY_OF_WEEK field indicating Tuesday.
int UNDECIMBER Value of the MONTH field indicating the thirteenth month of the year.
int WEDNESDAY Value of the DAY_OF_WEEK field indicating Wednesday.
int WEEK_OF_MONTH Field number for get and set indicating the week number within the current month.
int WEEK_OF_YEAR Field number for get and set indicating the week number within the current year.
int YEAR Field number for get and set indicating the year.
int ZONE_OFFSET Field number for get and set indicating the raw offset from GMT in milliseconds.

沒有留言:

張貼留言