未正确设置Java日历日期月份

时间:2011-01-10 17:21:57

标签: java android calendar

我创建了一个日历,其中suer可以滚动浏览上个月和下个月的日历,但我遇到的问题是当用户尝试滚动到上个月时。

当用户在第一次运行应用程序时点击上个月时,它可以正常工作,但是当用户再次点击它时,它不会将我发送给Calendar.Set()的值发送到100%正确甚至调试它然而实际的日历没有更新,因此返回与当前日期相同的月份!

这是我的代码。

@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();

    }

}

这是实际更新UI的代码。问题是onClick中的某个地方,因为它在下面的代码中返回错误的月份:

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);

}
  

私人enum月份{January,   二月,三月,四月,五月,六月,   七月,八月,九月,十月,   11月,12月};

4 个答案:

答案 0 :(得分:3)

除非我遗漏了某些东西,否则你实际上从未减少月份,除非是月份是1月。

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);

你有一个评论说减月,甚至还有日志评论说明你应该减月......但没有实际的减量:)

else {
    currentMonth--;
    mCurrentMonth.set(Calendar.MONTH, currentMonth);

答案 1 :(得分:2)

删除java日历并移至JodaTime

答案 2 :(得分:1)

要添加或减去一个月,请使用mCalendar.add(Calendar.MONTH, 1); mCalendar.add(Calendar.MONTH, -1);

答案 3 :(得分:0)

最好使用date4J jar而不是Java的Calendar或Date类。