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 updateSpinners() { boolean isLeapYear = false; int gregorianYear = mCurrentDate.get(Calendar.YEAR); int gregorianMonth = mCurrentDate.get(Calendar.MONTH) + 1; int gregorianDay = mCurrentDate.get(Calendar.DAY_OF_MONTH); int lunarDate[] = LunarUtil.calculateLunarByGregorian(gregorianYear, gregorianMonth, gregorianDay); int leapMonth = LunarUtil.leapMonth(lunarDate[0]); int monthIndexDisplay = lunarDate[1]; String lunarDateString = LunarUtil.getLunarDateString(mCurrentDate); if (leapMonth == 0) { monthIndexDisplay--; } else if (monthIndexDisplay < leapMonth && leapMonth != 0) { monthIndexDisplay--; } else if ((monthIndexDisplay == leapMonth) && (!lunarDateString.contains("闰"))) { monthIndexDisplay--; } if (leapMonth != 0) { mNumberOfMonths = 13; isLeapYear = true; } else { mNumberOfMonths = 12; } int monthCountDays = LunarUtil.daysOfALunarMonth(lunarDate[0], lunarDate[1]); if ((leapMonth != 0) && (monthIndexDisplay == leapMonth) && (!lunarDateString.contains("闰"))) { monthCountDays = LunarUtil.daysOfLeapMonthInLunarYear(lunarDate[0]); } // set the spinner ranges respecting the min and max dates if (mCurrentDate.equals(mMinDate)) { mDaySpinner.setDisplayedValues(null); mDaySpinner.setMinValue(lunarDate[2]); mDaySpinner.setMaxValue(monthCountDays); mDaySpinner.setWrapSelectorWheel(false); mMonthSpinner.setDisplayedValues(null); mMonthSpinner.setMinValue(monthIndexDisplay); mMonthSpinner.setMaxValue(mNumberOfMonths - 1); mMonthSpinner.setWrapSelectorWheel(false); } else if (mCurrentDate.equals(mMaxDate)) { mDaySpinner.setDisplayedValues(null); mDaySpinner.setMinValue(1); mDaySpinner.setMaxValue(lunarDate[2]); mDaySpinner.setWrapSelectorWheel(false); mMonthSpinner.setDisplayedValues(null); mMonthSpinner.setMinValue(0); mMonthSpinner.setMaxValue(monthIndexDisplay); mMonthSpinner.setWrapSelectorWheel(false); } else { mDaySpinner.setDisplayedValues(null); mDaySpinner.setMinValue(1); mDaySpinner.setMaxValue(monthCountDays); mDaySpinner.setWrapSelectorWheel(true); mMonthSpinner.setDisplayedValues(null); mMonthSpinner.setMinValue(0); mMonthSpinner.setMaxValue(mNumberOfMonths - 1); mMonthSpinner.setWrapSelectorWheel(true); } String[] displayedMonths = new String[mNumberOfMonths]; if (isLeapYear) { int i = 0; for (; i < leapMonth; i++) { displayedMonths[i] = mShortMonths[i]; } displayedMonths[leapMonth] = "闰" + chineseNumber[leapMonth - 1]; i++; for (; i < 13; i++) { displayedMonths[i] = mShortMonths[i - 1]; } } else { displayedMonths = Arrays.copyOfRange( mShortMonths, mMonthSpinner.getMinValue(), mMonthSpinner.getMaxValue() + 1); } mMonthSpinner.setDisplayedValues(displayedMonths); int max = mDaySpinner.getMaxValue(); int min = mDaySpinner.getMinValue(); String[] displayedDays = new String[max - min + 1]; LunarUtil lunar = new LunarUtil(); for (int i = min; i <= max; i++) { displayedDays[i - min] = LunarUtil.chneseStringOfALunarDay(i); } mDaySpinner.setDisplayedValues(displayedDays); // year spinner range does not change based on the current date int minGregorianYear = mMinDate.get(Calendar.YEAR); int minGregorianMonth = mMinDate.get(Calendar.MONTH) + 1; int minGregorianDay = mMinDate.get(Calendar.DAY_OF_MONTH); int minLunarDate[] = LunarUtil.calculateLunarByGregorian(minGregorianYear, minGregorianMonth, minGregorianDay); int maxGregorianYear = mMaxDate.get(Calendar.YEAR); int maxGregorianMonth = mMaxDate.get(Calendar.MONTH) + 1; int maxGregorianDay = mMaxDate.get(Calendar.DAY_OF_MONTH); int maxLunarDate[] = LunarUtil.calculateLunarByGregorian(maxGregorianYear, maxGregorianMonth, maxGregorianMonth); mYearSpinner.setMinValue(minLunarDate[0]); mYearSpinner.setMaxValue(maxLunarDate[0]); mYearSpinner.setWrapSelectorWheel(false); mYearSpinner.setValue(lunarDate[0]); mMonthSpinner.setValue(monthIndexDisplay); mDaySpinner.setValue(lunarDate[2]); }