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

发布于 2024-10-11 06:28:32 字数 3132 浏览 1 评论 0原文

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

当用户第一次运行应用程序时单击上个月时,它可以工作,但是当用户再次单击时,它不会否认我发送到 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);

}

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你对谁都笑 2024-10-18 06:28:32

除非我遗漏了一些东西,否则你实际上永远不会减少月份,除非月份是一月。

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

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);
栖竹 2024-10-18 06:28:32

删除 java Calendar 并移至 JodaTime

Drop java Calendar and move to JodaTime

甲如呢乙后呢 2024-10-18 06:28:32

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

To add or substract a month use mCalendar.add(Calendar.MONTH, 1); mCalendar.add(Calendar.MONTH, -1);

没有你我更好 2024-10-18 06:28:32

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

Its better to use date4J jar instead of Calendar or Date classes of Java.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文