Code example for Calendar

Methods: get, set

0
     * Get current year 
     * 
     * @return The current year 
     */ 
    public int getCurrentYear() { 
        return mDate.get(Calendar.YEAR);
    } 
 
    /** 
     * Set current year 
     * 
     * @param year The current year 
     */ 
    public void setCurrentYear(int year) {
        if (!mInitialising && year == getCurrentYear()) {
            return; 
        } 
        mDate.set(Calendar.YEAR, year);
        updateDateControl(); 
        onDateTimeChanged(); 
    } 
 
    /** 
     * Get current month in the year 
     * 
     * @return The current month in the year 
     */ 
    public int getCurrentMonth() { 
        return mDate.get(Calendar.MONTH);
    } 
 
    /** 
     * Set current month in the year 
     * 
     * @param month The month in the year 
     */ 
    public void setCurrentMonth(int month) {
        if (!mInitialising && month == getCurrentMonth()) {
            return; 
        } 
        mDate.set(Calendar.MONTH, month);
        updateDateControl(); 
        onDateTimeChanged(); 
    } 
 
    /** 
     * Get current day of the month 
     * 
     * @return The day of the month 
     */ 
    public int getCurrentDay() { 
        return mDate.get(Calendar.DAY_OF_MONTH);
    } 
 
    /** 
     * Set current day of the month 
     * 
     * @param dayOfMonth The day of the month 
     */ 
    public void setCurrentDay(int dayOfMonth) {
        if (!mInitialising && dayOfMonth == getCurrentDay()) {
            return; 
        } 
        mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        updateDateControl(); 
        onDateTimeChanged(); 
    } 
 
    /** 
     * Get current hour in 24 hour mode, in the range (0~23) 
     * @return The current hour in 24 hour mode 
     */ 
    public int getCurrentHourOfDay() { 
        return mDate.get(Calendar.HOUR_OF_DAY);
    } 
 
    private int getCurrentHour() { 
        if (mIs24HourView){
            return getCurrentHourOfDay(); 
        } else { 
            int hour = getCurrentHourOfDay();
            if (hour > HOURS_IN_HALF_DAY) {
                return hour - HOURS_IN_HALF_DAY;
            } else { 
                return hour == 0 ? HOURS_IN_HALF_DAY : hour;
            } 
        } 
    } 
 
    /** 
     * Set current hour in 24 hour mode, in the range (0~23) 
     * 
     * @param hourOfDay 
     */ 
    public void setCurrentHour(int hourOfDay) {
        if (!mInitialising && hourOfDay == getCurrentHourOfDay()) {
            return; 
        } 
        mDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
        if (!mIs24HourView) {
            if (hourOfDay >= HOURS_IN_HALF_DAY) {
                mIsAm = false;
                if (hourOfDay > HOURS_IN_HALF_DAY) {
                    hourOfDay -= HOURS_IN_HALF_DAY;
                } 
            } else { 
                mIsAm = true;
                if (hourOfDay == 0) {
                    hourOfDay = HOURS_IN_HALF_DAY;
                } 
            } 
            updateAmPmControl(); 
        } 
        mHourSpinner.setValue(hourOfDay);
        onDateTimeChanged(); 
    } 
 
    /** 
     * Get currentMinute 
     * 
     * @return The Current Minute 
     */ 
    public int getCurrentMinute() { 
        return mDate.get(Calendar.MINUTE);
    } 
 
    /** 
     * Set current minute 
     */ 
    public void setCurrentMinute(int minute) {
        if (!mInitialising && minute == getCurrentMinute()) {
            return; 
        } 
        mMinuteSpinner.setValue(minute);
        mDate.set(Calendar.MINUTE, minute);
        onDateTimeChanged(); 
    } 
 
    /** 
     * @return true if this is in 24 hour view else false.