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 的代码。问题出在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
除非我遗漏了一些东西,否则你实际上永远不会减少月份,除非月份是一月。
你有一条评论说要减少月份,甚至有一条日志评论说你应该减少月份......但没有实际的减少:)
Unless I'm missing something, you never actually decrement the month, except when the month is January.
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 :)
删除 java Calendar 并移至 JodaTime
Drop java Calendar and move to JodaTime
要添加或减去一个月,请使用
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);
最好使用 date4J jar 而不是 Java 的 Calendar 或 Date 类。
Its better to use date4J jar instead of Calendar or Date classes of Java.