2011-01-10 72 views
0

我已经创建了一个日历,其中suer可以滚动浏览前一个月和下一个月的日历,但我遇到的问题是用户尝试滚动到前一个月。Java日历日期月份设置不正确

当用户第一次点击到前一个月运行应用程序时,它可以工作,但是当用户再次点击它时,它并不会妨碍我发送给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的代码。问题是somwhere在onclick,因为它返回错误的月份在下面的代码:

私人无效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); 

} 

私人枚举月{一月, 二月,三月,四月,五月,六月, 七月,八月,九月,十月, 十一月,十二月};

+1

这不是你如何发布代码到Stackoverflow。每行代码应该缩进4个空格。 – Pointy 2011-01-10 17:26:05

+0

现在修正了队友谢谢 – jonney 2011-01-10 17:30:40

回答

2

降Java日历和移动JodaTime

+0

在同一行,放弃java并移动到c#? – KevinDTimm 2011-01-10 17:42:43

+0

@KevinDTimm。哈哈 - 现在,我们不要进入那个古老的板栗。 – 2011-01-10 17:46:57

3

除非我失去了一些东西,你从来没有真正递减月份,除了当月份是一月。

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

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

0

它更好地使用date4J而不是日历或日期类的Java。