日历返回不正确的MONTH,YEAR和DAY
我为我的应用程序日历使用了MaterialCalendarView。 我想读取用户点击日历上的下一个/上一个的月份和年份。 这是我的代码:
calendarView = (CalendarView) view.findViewById(R.id.calendarView);
calendarView.setOnPreviousButtonClickListener(new OnNavigationButtonClickListener() {
@Override
public void onClick() {
Calendar calendar = calendarView.getCurrentPageDate();
int previousMonth = calendar.MONTH;
int previousYear = calendar.YEAR;
Log.i(TAG, "--- previous month: " + previousMonth + "/" + previousYear + " - " + calendar);
}
});
calendarView.setOnForwardButtonClickListener(new OnNavigationButtonClickListener() {
@Override
public void onClick() {
Calendar calendar = calendarView.getCurrentPageDate();
int nextMonth = calendar.MONTH;
int nextYear = calendar.YEAR;
Log.i(TAG, "--- next month: " + nextMonth + "/" + nextYear + " - " + calendar);
}
});
但是,当我点击下一个/上一个箭头时,返回给我的MONTH和YEAR值不正确。 例如,在这个例子中,它的2018年1月。上个月应该是11年,而应该是2017年,下个月应该是1年和2018年。
但是,对于先前在日志中的点击,我看到:
I / CalendarFragment:---上个月:2/1 - java.util.GregorianCalendar [time = ?, areFieldsSet = false,areAllFieldsSet = true,lenient = true,zone = java.util.SimpleTimeZone [id = GMT,offset = 0,dstSavings = 3600000,useDaylight =假,startYear = 0,STARTMODE = 0,startMonth = 0,朝九特派= 0,startDayOfWeek = 0,开始时间= 0,startTimeMode = 0,endMode = 0,endMonth = 0,endday指定= 0,一个endDayOfWeek = 0,结束时间= 0,endTimeMode = 0],Firstdayofweek可= 1,minimalDaysInFirstWeek = 1,ERA = 1,YEAR = 2017,月= 11,WEEK_OF_YEAR = 1,WEEK_OF_MONTH = 1,DAY_OF_MONTH = 1,DAY_OF_YEAR = 1,DAY_OF_WEEK = 3,DAY_OF_WEEK_IN_MONTH = 1,AM_PM = 0,HOUR = 0,HOUR_OF_DAY = 0,MINUTE = 0,SECOND = 0,微差= 0,ZONE_OFFSET = 0,DST_OFFSET = 0]
对于下一次点击,我看到:
I / CalendarFragment:--- next month:2/1 - java.util.GregorianCalendar [time = ?, areFieldsSet = false,areAllFieldsSet = true,lenient = true,zone = java.util.SimpleTimeZone [id = GMT,offset = 0,dstSavings = 3600000,useDaylight =假,startYear = 0,STARTMODE = 0,startMonth = 0,朝九特派= 0,startDayOfWeek = 0,开始时间= 0,startTimeMode = 0,endMode = 0,endMonth = 0,endday指定= 0,一个endDayOfWeek = 0,结束时间= 0,endTimeMode = 0],Firstdayofweek可= 1,minimalDaysInFirstWeek = 1,ERA = 1,YEAR = 2018,月= 1,WEEK_OF_YEAR = 1,WEEK_OF_MONTH = 1,DAY_OF_MONTH = 1,DAY_OF_YEAR = 1,DAY_OF_WEEK = 3,DAY_OF_WEEK_IN_MONTH = 1,AM_PM = 0,HOUR = 0,HOUR_OF_DAY = 0,MINUTE = 0,SECOND = 0,微差= 0,ZONE_OFFSET = 0,DST_OFFSET = 0]
如果您看到日志,则日历实例值具有正确的MONTH和YEAR。 但是当我在外面访问它时,返回2和1.为什么?
同样的事情发生在我做setOnDayClickListener()时
calendarView.setOnDayClickListener(new OnDayClickListener() {
@Override
public void onDayClick(EventDay eventDay) {
Calendar clickedDayCalendar = eventDay.getCalendar();
Log.i(TAG, "------- clicked day: " + clickedDayCalendar.DAY_OF_MONTH + " - " + clickedDayCalendar);
}
});
无论我在1月点击哪一天,它都会让我点击5天。以下是日志:
I / CalendarFragment:------- clicked day:5 - java.util.GregorianCalendar [time = ?, areFieldsSet = false,areAllFieldsSet = true,lenient = true,zone = java.util.SimpleTimeZone [id = GMT,偏移量= 0,dstSavings = 3600000,useDaylight =假,startYear = 0,STARTMODE = 0,startMonth = 0,朝九特派= 0,startDayOfWeek = 0,开始时间= 0,startTimeMode = 0,endMode = 0,endMonth = 0,endday指定= 0,一个endDayOfWeek = 0,结束时间= 0,endTimeMode = 0],Firstdayofweek可= 1,minimalDaysInFirstWeek = 1,ERA = 1,YEAR = 2018,MONTH = 0,WEEK_OF_YEAR = 2,WEEK_OF_MONTH = 2,DAY_OF_MONTH = 12,DAY_OF_YEAR = 12 ,DAY_OF_WEEK = 6,DAY_OF_WEEK_IN_MONTH = 2,AM_PM = 0,HOUR = 10,HOUR_OF_DAY = 0,MINUTE = 0,SECOND = 0,微差= 0,ZONE_OFFSET = 0,DST_OFFSET = 0]
I / CalendarFragment:------- clicked day:5 - java.util.GregorianCalendar [time = ?, areFieldsSet = false,areAllFieldsSet = true,lenient = true,zone = java.util.SimpleTimeZone [id = GMT,偏移量= 0,dstSavings = 3600000,useDaylight =假,startYear = 0,STARTMODE = 0,startMonth = 0,朝九特派= 0,startDayOfWeek = 0,开始时间= 0,startTimeMode = 0,endMode = 0,endMonth = 0,endday指定= 0,一个endDayOfWeek = 0,结束时间= 0,endTimeMode = 0],Firstdayofweek可= 1,minimalDaysInFirstWeek = 1,ERA = 1,YEAR = 2018,MONTH = 0,WEEK_OF_YEAR = 3,WEEK_OF_MONTH = 3,DAY_OF_MONTH = 20,DAY_OF_YEAR = 20 ,DAY_OF_WEEK = 7,DAY_OF_WEEK_IN_MONTH = 3,AM_PM = 1,HOUR = 3,HOUR_OF_DAY = 0,MINUTE = 0,SECOND = 0,微差= 0,ZONE_OFFSET = 0,DST_OFFSET = 0]
任何想法为什么发生这种情况? 或者是什么原因造成的?
MONTH和YEAR是公共静态int变量。 所以用Calendar类访问它们。
Calendar calendar = calendarView.getCurrentPageDate();
int previousMonth = calendar.get(Calendar.MONTH) + 1;
int previousYear = calendar.get(Calendar.YEAR);
Log.i(TAG, "--- previous month: " + previousMonth + "/" + previousYear);
链接地址: http://www.djcxy.com/p/6237.html
上一篇: Calendar returns incorrect MONTH, YEAR and DAY
下一篇: Why don't I get the year right using SimpleDateFormat in java?