【问题标题】:Java Calendar date month not set correctlyJava 日历日期月份未正确设置
【发布时间】:2011-06-06 16:16:10
【问题描述】:

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

当用户在应用程序第一次运行时单击上个月时,它可以工作,但是当用户再次单击时,它并没有忽略我发送给 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 的某个地方,因为它在下面的代码中返回了错误的月份:

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

}

私有枚举 Months { 一月, 二月,三月,四月,五月,六月, 七月,八月,九月,十月, 11 月、12 月 };

【问题讨论】:

  • 这不是您将代码发布到 Stackoverflow 的方式。每行代码应该缩进 4 个空格。

标签: java android calendar


【解决方案1】:

删除 java 日历并移至 JodaTime

【讨论】:

【解决方案2】:

除非我遗漏了什么,否则你永远不会真正减少月份,除非月份是一月。

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

【讨论】:

  • 我想我在尝试某些东西时错误地删除了它,但它最初是他们的。我又加回来了,日期还是没变
  • 当我第一次点击上一个按钮时,它将月份设置为 11(十二月),因为它是本月的一月,当我再次点击时,currentMonth 成功减少 gtom 11 到 10,如图所示我的 log.d() 然后我将 Month 设置为值 10 的当前月份,当我稍后在我的代码中执行 log.d 以获取当前月份设置时,它仍然没有正确设置它。
  • @jonney 您的日志显示您将当前月份设置为 10。您的意思是它没有正确设置?
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    最好使用date4J jar 代替 Java 的 Calendar 或 Date 类。

    【讨论】:

    • 乔达时间是一种使用方式
    • 请查看我提供的链接并查看 Joda time date4j.net/#Joda的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多