Code example for Calendar

Methods: after, before, set, setTimeInMillis

0
 
            mCalendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
                @Override 
                public void onSelectedDayChange( 
                    CalendarView view, int year, int month, int monthDay) {
                    mTempDate.set(year, month, monthDay);
                    setDate(mTempDate);
                    notifyDateChanged(); 
                } 
            }); 
 
            mPickers.addView(mCalendar);
        } else { 
          // If the screen is more wide than high, we are displaying daye and time spinners, 
          // and if there is no calendar displayed, 
          // we should display the fields in one row. 
            if (mScreenWidth > mScreenHeight && mState == PickersState.DATETIME) {
                mSpinners.setOrientation(LinearLayout.HORIZONTAL);
            } 
            mCalendar = null;
        } 
 
        // Find the initial date from the constructor arguments. 
        try { 
            if (!dateTimeValue.equals("")) {
                mTempDate.setTime(new SimpleDateFormat(dateFormat).parse(dateTimeValue));
            } else { 
                mTempDate.setTimeInMillis(System.currentTimeMillis());
            } 
        } catch (Exception ex) {
            Log.e(LOGTAG, "Error parsing format string: " + ex);
            mTempDate.setTimeInMillis(System.currentTimeMillis());
        } 
 
        // Initialize all spinners. 
        mDaySpinner = setupSpinner(R.id.day, 1,
                                   mTempDate.get(Calendar.DAY_OF_MONTH));
        mDaySpinner.setFormatter(TWO_DIGIT_FORMATTER);
        mDaySpinnerInput = (EditText) mDaySpinner.getChildAt(1);
 
        mMonthSpinner = setupSpinner(R.id.month, 1,
                                     mTempDate.get(Calendar.MONTH));
        mMonthSpinner.setFormatter(TWO_DIGIT_FORMATTER);
        mMonthSpinner.setDisplayedValues(mShortMonths);
        mMonthSpinnerInput = (EditText) mMonthSpinner.getChildAt(1);
 
        mWeekSpinner = setupSpinner(R.id.week, 1,
                                    mTempDate.get(Calendar.WEEK_OF_YEAR));
        mWeekSpinner.setFormatter(TWO_DIGIT_FORMATTER);
        mWeekSpinnerInput = (EditText) mWeekSpinner.getChildAt(1);
 
        mYearSpinner = setupSpinner(R.id.year, DEFAULT_START_YEAR,
                                    DEFAULT_END_YEAR);
        mYearSpinnerInput = (EditText) mYearSpinner.getChildAt(1);
 
        mAMPMSpinner = setupSpinner(R.id.ampm, 0, 1);
        mAMPMSpinner.setFormatter(TWO_DIGIT_FORMATTER);
 
        if (mIs12HourMode) {
            mHourSpinner = setupSpinner(R.id.hour, 1, 12);
            mAMPMSpinnerInput = (EditText) mAMPMSpinner.getChildAt(1);
            mAMPMSpinner.setDisplayedValues(mShortAMPMs); 
        } else { 
            mHourSpinner = setupSpinner(R.id.hour, 0, 23);
            mAMPMSpinnerInput = null;
        } 
 
        mHourSpinner.setFormatter(TWO_DIGIT_FORMATTER);
        mHourSpinnerInput = (EditText) mHourSpinner.getChildAt(1);
 
        mMinuteSpinner = setupSpinner(R.id.minute, 0, 59);
        mMinuteSpinner.setFormatter(TWO_DIGIT_FORMATTER);
        mMinuteSpinnerInput = (EditText) mMinuteSpinner.getChildAt(1);
 
        // The order in which the spinners are displayed are locale-dependent 
        reorderDateSpinners(); 
        // Set the date to the initial date. Since this date can come from the user, 
        // it can fire an exception (out-of-bound date) 
        try { 
          updateDate(mTempDate);
        } catch (Exception ex) { }
 
        // Display only the pickers needed for the current state. 
        displayPickers(); 
    } 
 
    public NumberPicker setupSpinner(int id, int min, int max) {
        NumberPicker mSpinner = (NumberPicker) findViewById(id);
        mSpinner.setMinValue(min);
        mSpinner.setMaxValue(max);
        mSpinner.setOnValueChangedListener(mOnChangeListener);
        mSpinner.setOnLongPressUpdateInterval(100);
        return mSpinner;
    } 
 
    public long getTimeInMillis(){ 
        return mCurrentDate.getTimeInMillis();
    } 
 
    private void reorderDateSpinners() { 
        mDateSpinners.removeAllViews();
        char[] order = DateFormat.getDateFormatOrder(getContext());
        final int spinnerCount = order.length;
        for (int i = 0; i < spinnerCount; i++) {
            switch (order[i]) {
                case DateFormat.DATE:
                    mDateSpinners.addView(mDaySpinner);
                    break; 
                case DateFormat.MONTH:
                    mDateSpinners.addView(mMonthSpinner);
                    break; 
                case DateFormat.YEAR:
                    mDateSpinners.addView(mYearSpinner);
                    break; 
                default: 
                    throw new IllegalArgumentException();
            } 
        } 
        mDateSpinners.addView(mWeekSpinner);
    } 
 
    private void setDate(Calendar calendar){
        mCurrentDate = mTempDate;
        if (mCurrentDate.before(mMinDate)) {
            mCurrentDate.setTimeInMillis(mMinDate.getTimeInMillis());
        } else if (mCurrentDate.after(mMaxDate)) {
            mCurrentDate.setTimeInMillis(mMaxDate.getTimeInMillis());
        } 
    } 
 
    private void updateInputState() { 
        InputMethodManager inputMethodManager = (InputMethodManager)