Java Calendar date month not set correctly
i have created a calendar where a suer can scroll through previous and next months calendar but the issue i am having is when the user tries to scroll into the previous month.
when the user clicks to the previous month the first time the app is run, it works but when the user clicks again it doesnt dispite the value i am sending to the Calendar.Set() being 100% correct and even debugged it yet the actual calendar doesnt update and therefore returns me the same month as the current one!
here is my code below.
@Override
public void onClick(View v) {
// get current month
int currentMonth = mCurrentMonth.get(Calendar.MONTH);
Log.d(TAG, "day = " + mCurrentMonth.get(Calendar.DAY_OF_MONTH));
Log.d(TAG, "currentMonth in onClick = " + currentMonth);
if (v == mPreviousMonthButton) {
Log.d(TAG, "mPreviousMonthButton CLICKED ");
// if current month is january
// decrement the current year and set month to december
if (currentMonth == Calendar.JANUARY) {
int currentYear = mCurrentMonth.get(Calendar.YEAR);
mCurrentMonth.set(Calendar.YEAR, currentYear - 1);
mCurrentMonth.set(Calendar.MONTH, Calendar.DECEMBER);
} else {
// else decrement the month
Log.d(TAG, "currentMonth-- = " + currentMonth);
mCurrentMonth.set(Calendar.MONTH, currentMonth);
Log.d(TAG,
"month in previus button = "
+ mCurrentMonth.get(Calendar.MONTH));
}
// save the month
setDateForMonth();
} else if (v == mNextMonthButton) {
Log.d(TAG, "mNextMonthButton CLICKED ");
if (currentMonth == Calendar.DECEMBER) {
int currentYear = mCurrentMonth.get(Calendar.YEAR);
mCurrentMonth.set(Calendar.YEAR, currentYear + 1);
mCurrentMonth.set(Calendar.MONTH, Calendar.JANUARY);
} else {
currentMonth--;
mCurrentMonth.set(Calendar.MONTH, currentMonth + 1);
Log.d(TAG, "currentMonth++ = " + currentMonth + 1);
Log.d(TAG,
"month in next button = "
+ mCurrentMonth.get(Calendar.MONTH));
}
// save the month
setDateForMonth();
}
}
here is the code that actually updates the UI. the problem is somwhere in the onClick as it returns the wrong month in the code below:
private void setDateForMonth() {
monthList.clear();
Log.d(TAG, ".........setDateForMonth...........");
Log.d(TAG, "....................");
Log.d(TAG, "month = " + mCurrentMonth.get(Calendar.MONTH));
Log.d(TAG, "year = " + mCurrentMonth.get(Calendar.YEAR));
CalendarMonth[] months = CalendarUtils
.constructMonthViewArray(mCurrentMonth);
for (int i = 0; i < months.length; i++) {
monthList.add(months[i]);
Log.d(TAG, monthList.get(i).getDay());
}
Log.d(TAG, "....................");
mAdapter = new CalendarMonthAdapter(mContext, monthList);
mMonthGridView.setAdapter(mAdapter);
Months[] month = Months.values();
String currentMonth = month[mCurrentMonth.get(Calendar.MONTH)]
.toString();
String year = Integer.toString(mCurrentMonth.get(Calendar.YEAR));
mMonthLabel.setText(currentMonth + " " + year);
}
private enum Months { January, February, March开发者_开发百科, April, May, June, July, August, September, October, November, December };
Unless I'm missing something, you never actually decrement the month, except when the month is January.
int currentMonth = mCurrentMonth.get(Calendar.MONTH);
...
if (currentMonth == Calendar.JANUARY) {
int currentYear = mCurrentMonth.get(Calendar.YEAR);
mCurrentMonth.set(Calendar.YEAR, currentYear - 1);
mCurrentMonth.set(Calendar.MONTH, Calendar.DECEMBER);
}
else {
// else decrement the month
Log.d(TAG, "currentMonth-- = " + currentMonth);
mCurrentMonth.set(Calendar.MONTH, currentMonth);
You've got a comment that says to decrement the month, and even a log comment that says you should be decrementing the month... but no actual decrement :)
else {
currentMonth--;
mCurrentMonth.set(Calendar.MONTH, currentMonth);
Drop java Calendar and move to JodaTime
To add or substract a month use mCalendar.add(Calendar.MONTH, 1);
mCalendar.add(Calendar.MONTH, -1);
Its better to use date4J jar instead of Calendar or Date classes of Java.
精彩评论