gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:40:07 26 4
gpt4 key购买 nike

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

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

最佳答案

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

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

关于Java 日历日期月份设置不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4649522/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com