Code example for Calendar

Methods: clear, clone, get, getTimeInMillis, set

0
        if (mCurrentDate == null) {
            mCurrentDate = Calendar.getInstance();
        } 
        mCurrentDate.clear();
        mCurrentDate.set(year, month, 1);
        if (mCurrentDate.getTimeInMillis() < mMinDate.getTimeInMillis()) {
            mCurrentDate = (Calendar) mMinDate.clone();
        } else if (mCurrentDate.getTimeInMillis() > mMaxDate.getTimeInMillis()) {
            mCurrentDate = (Calendar) mMaxDate.clone();
        } 
        updateSpinners(); 
    } 
 
    private static Calendar monthsToCalendar(long months) {
        int year = (int)Math.min(months / 12 + 1970, Integer.MAX_VALUE);
        int month = (int) (months % 12);
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(year, month, 1);
        return cal;
    } 
 
    @Override 
    public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
        onPopulateAccessibilityEvent(event);
        return true; 
    } 
 
    @Override 
    public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
        super.onPopulateAccessibilityEvent(event);
 
        final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR;
        String selectedDateUtterance = DateUtils.formatDateTime(getContext(),
                mCurrentDate.getTimeInMillis(), flags);
        event.getText().add(selectedDateUtterance);
    } 
 
    @Override 
    protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    } 
 
    /** 
     * Initialize the state. If the provided values designate an inconsistent 
     * date the values are normalized before updating the spinners. 
     * 
     * @param year The initial year. 
     * @param month The initial month <strong>starting from zero</strong>. 
     * @param onMonthChangedListener How user is notified date is changed by 
     *            user, can be null. 
     */ 
    public void init(int year, int month, OnMonthChangedListener onMonthChangedListener) {
        setCurrentDate(year, month);
        updateSpinners(); 
        mMonthChangedListener = onMonthChangedListener;
    } 
 
    private boolean isNewDate(int year, int month) {
        return (mCurrentDate.get(Calendar.YEAR) != year
                || mCurrentDate.get(Calendar.MONTH) != month);
    } 
 
    private void updateSpinners() { 
        // set the spinner ranges respecting the min and max dates 
        mMonthSpinner.setDisplayedValues(null);
        mMonthSpinner.setMinValue(0);
        mMonthSpinner.setMaxValue(MONTHS_NUMBER - 1);
        if (mCurrentDate.get(Calendar.YEAR) == mMinDate.get(Calendar.YEAR)) {
            mMonthSpinner.setMinValue(mMinDate.get(Calendar.MONTH));
            mMonthSpinner.setWrapSelectorWheel(false);
        } else if (mCurrentDate.get(Calendar.YEAR) == mMaxDate.get(Calendar.YEAR)) {
            mMonthSpinner.setMaxValue(mMaxDate.get(Calendar.MONTH));
            mMonthSpinner.setWrapSelectorWheel(false);
        } else { 
            mMonthSpinner.setWrapSelectorWheel(true);
        } 
 
        // make sure the month names are a zero based array 
        // with the months in the month spinner 
        String[] displayedValues = Arrays.copyOfRange(mShortMonths,
                mMonthSpinner.getMinValue(), mMonthSpinner.getMaxValue() + 1);
        mMonthSpinner.setDisplayedValues(displayedValues);
 
        // year spinner range does not change based on the current date 
        mYearSpinner.setMinValue(mMinDate.get(Calendar.YEAR));
        mYearSpinner.setMaxValue(mMaxDate.get(Calendar.YEAR));
        mYearSpinner.setWrapSelectorWheel(false);
 
        // set the spinner values 
        mYearSpinner.setValue(mCurrentDate.get(Calendar.YEAR));
        mMonthSpinner.setValue(mCurrentDate.get(Calendar.MONTH));
    } 
 
    /** 
     * @return The selected year. 
     */