Code example for Calendar

Methods: after, before, equals, get, getActualMaximum, getActualMinimum, setTimeInMillis

0
        } 
    } 
 
    private void updateSpinners() { 
        if (mDayEnabled) {
            if (mCurrentDate.equals(mMinDate)) {
                mDaySpinner.setMinValue(mCurrentDate.get(Calendar.DAY_OF_MONTH));
                mDaySpinner.setMaxValue(mCurrentDate.getActualMaximum(Calendar.DAY_OF_MONTH));
            } else if (mCurrentDate.equals(mMaxDate)) {
                mDaySpinner.setMinValue(mCurrentDate.getActualMinimum(Calendar.DAY_OF_MONTH));
                mDaySpinner.setMaxValue(mCurrentDate.get(Calendar.DAY_OF_MONTH));
            } else { 
                mDaySpinner.setMinValue(1);
                mDaySpinner.setMaxValue(mCurrentDate.getActualMaximum(Calendar.DAY_OF_MONTH));
            } 
            mDaySpinner.setValue(mCurrentDate.get(Calendar.DAY_OF_MONTH));
        } 
 
        if (mWeekEnabled) {
            mWeekSpinner.setMinValue(1);
            mWeekSpinner.setMaxValue(mCurrentDate.getActualMaximum(Calendar.WEEK_OF_YEAR));
            mWeekSpinner.setValue(mCurrentDate.get(Calendar.WEEK_OF_YEAR));
        } 
 
        if (mMonthEnabled) {
            mMonthSpinner.setDisplayedValues(null);
            if (mCurrentDate.equals(mMinDate)) {
                mMonthSpinner.setMinValue(mCurrentDate.get(Calendar.MONTH));
                mMonthSpinner.setMaxValue(mCurrentDate.getActualMaximum(Calendar.MONTH));
            } else if (mCurrentDate.equals(mMaxDate)) {
                mMonthSpinner.setMinValue(mCurrentDate.getActualMinimum(Calendar.MONTH));
                mMonthSpinner.setMaxValue(mCurrentDate.get(Calendar.MONTH));
            } else { 
                mMonthSpinner.setMinValue(Calendar.JANUARY);
                mMonthSpinner.setMaxValue(Calendar.DECEMBER);
            } 
 
            String[] displayedValues = Arrays.copyOfRange(mShortMonths,
                    mMonthSpinner.getMinValue(), mMonthSpinner.getMaxValue() + 1);
            mMonthSpinner.setDisplayedValues(displayedValues);
            mMonthSpinner.setValue(mCurrentDate.get(Calendar.MONTH));
        } 
 
        if (mYearEnabled) {
            mYearSpinner.setMinValue(mMinDate.get(Calendar.YEAR));
            mYearSpinner.setMaxValue(mMaxDate.get(Calendar.YEAR));
            mYearSpinner.setValue(mCurrentDate.get(Calendar.YEAR));
        } 
 
        if (mHourEnabled) {
            if (mIs12HourMode) {
                mHourSpinner.setValue(mCurrentDate.get(Calendar.HOUR));
                mAMPMSpinner.setValue(mCurrentDate.get(Calendar.AM_PM));
                mAMPMSpinner.setDisplayedValues(mShortAMPMs);
            } else { 
                mHourSpinner.setValue(mCurrentDate.get(Calendar.HOUR_OF_DAY));
            } 
        } 
        if (mMinuteEnabled) {
            mMinuteSpinner.setValue(mCurrentDate.get(Calendar.MINUTE));
        } 
    } 
 
    private void updateCalendar() { 
        if (mCalendarEnabled){
            mCalendar.setDate(mCurrentDate.getTimeInMillis(), false, false);
        } 
    } 
 
    private void notifyDateChanged() { 
        sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
    } 
 
    public void toggleCalendar(boolean shown) {
        if ((mState != PickersState.DATE && mState != PickersState.DATETIME) ||
            Build.VERSION.SDK_INT < 11 || mScreenWidth < SCREEN_SIZE_THRESHOLD) {
            if (DEBUG) Log.d(LOGTAG,"Cannot display calendar on this device, in this state" +
                ": screen width :"+mScreenWidth);
            return; 
        } 
        if (shown){
            mCalendarEnabled = true;
            mCalendar.setVisibility(VISIBLE);
            setYearShown(false); 
            setWeekShown(false); 
            setMonthShown(false); 
            setDayShown(false); 
        } else { 
            mCalendar.setVisibility(GONE);
            setYearShown(true); 
            setMonthShown(true); 
            setDayShown(true); 
            mSpinners.setOrientation(LinearLayout.HORIZONTAL);
            mCalendarEnabled = false;
        } 
    } 
 
    private void setYearShown(boolean shown) {
        if (shown) {
            toggleCalendar(false); 
            mYearSpinner.setVisibility(VISIBLE);
            mYearEnabled = true;
        } else { 
            mYearSpinner.setVisibility(GONE);
            mYearEnabled = false;
        } 
    } 
 
    private void setWeekShown(boolean shown) {
        if (shown) {
            toggleCalendar(false); 
            mWeekSpinner.setVisibility(VISIBLE);
            mWeekEnabled = true;
        } else { 
            mWeekSpinner.setVisibility(GONE);
            mWeekEnabled = false;
        } 
    } 
 
    private void setMonthShown(boolean shown) {
        if (shown) {
            toggleCalendar(false); 
            mMonthSpinner.setVisibility(VISIBLE);
            mMonthEnabled = true;
        } else { 
            mMonthSpinner.setVisibility(GONE);
            mMonthEnabled = false;
        } 
    } 
 
    private void setDayShown(boolean shown) {
        if (shown) {
            toggleCalendar(false); 
            mDaySpinner.setVisibility(VISIBLE);
            mDayEnabled = true;
        } else { 
            mDaySpinner.setVisibility(GONE);
            mDayEnabled = false;
        } 
    } 
 
    private void set12HourShown(boolean shown) {
        if (shown) {
            mAMPMSpinner.setVisibility(VISIBLE);
        } else { 
            mAMPMSpinner.setVisibility(GONE);
        } 
    } 
 
    private void setHourShown(boolean shown) {
        if (shown) {
            mHourSpinner.setVisibility(VISIBLE);
            mHourEnabled= true;
        } else { 
            mHourSpinner.setVisibility(GONE);
            mAMPMSpinner.setVisibility(GONE);
            mTimeSpinners.setVisibility(GONE);
            mHourEnabled = false;
        } 
    } 
 
    private void setMinuteShown(boolean shown) {
        if (shown) {
            mMinuteSpinner.setVisibility(VISIBLE);
            mTimeSpinners.findViewById(R.id.mincolon).setVisibility(VISIBLE);
            mMinuteEnabled= true;
        } else { 
            mMinuteSpinner.setVisibility(GONE);
            mTimeSpinners.findViewById(R.id.mincolon).setVisibility(GONE);
            mMinuteEnabled = false;
        } 
    } 
 
    private void setCurrentLocale(Locale locale) {
        if (locale.equals(mCurrentLocale)) {
            return; 
        } 
 
        mCurrentLocale = locale;
        mIs12HourMode = !DateFormat.is24HourFormat(getContext());
        mTempDate = getCalendarForLocale(mTempDate, locale);
        mMinDate = getCalendarForLocale(mMinDate, locale);
        mMaxDate = getCalendarForLocale(mMaxDate, locale);
        mCurrentDate = getCalendarForLocale(mCurrentDate, locale);
 
        mNumberOfMonths = mTempDate.getActualMaximum(Calendar.MONTH) + 1;
 
        mShortAMPMs = new String[2];
        mShortAMPMs[0] = DateUtils.getAMPMString(Calendar.AM);
        mShortAMPMs[1] = DateUtils.getAMPMString(Calendar.PM);
 
        mShortMonths = new String[mNumberOfMonths];
        for (int i = 0; i < mNumberOfMonths; i++) {
            mShortMonths[i] = DateUtils.getMonthString(Calendar.JANUARY + i,
                    DateUtils.LENGTH_MEDIUM);
        } 
    } 
 
    private Calendar getCalendarForLocale(Calendar oldCalendar, Locale locale) {
        if (oldCalendar == null) {
            return Calendar.getInstance(locale);
        } else { 
            final long currentTimeMillis = oldCalendar.getTimeInMillis();
            Calendar newCalendar = Calendar.getInstance(locale);
            newCalendar.setTimeInMillis(currentTimeMillis);
            return newCalendar;
        } 
    } 
 
    public void updateDate(Calendar calendar) {
        if (mCurrentDate.equals(calendar)) {
            return; 
        } 
        mCurrentDate.setTimeInMillis(calendar.getTimeInMillis());
        if (mCurrentDate.before(mMinDate)) {
            mCurrentDate.setTimeInMillis(mMinDate.getTimeInMillis());
        } else if (mCurrentDate.after(mMaxDate)) {
            mCurrentDate.setTimeInMillis(mMaxDate.getTimeInMillis());
        } 
        updateSpinners(); 
        notifyDateChanged(); 
    }