Calendar returns incorrect MONTH, YEAR and DAY
I am using MaterialCalendarView for my app calendar. I want to read the month and year when the user clicks next/previous on the calendar. Here is my code:
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);
}
});
However when I click the next/previous arrows the MONTH and YEAR values returned to me are incorrect. So for example at this instance its JAN 2018. Previous month should be 11 and year should be 2017, and next month should be 1 and 2018.
However for previous click in the logs, I see:
I/CalendarFragment: --- previous month: 2/1 - java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=java.util.SimpleTimeZone[id=GMT,offset=0,dstSavings=3600000,useDaylight=false,startYear=0,startMode=0,startMonth=0,startDay=0,startDayOfWeek=0,startTime=0,startTimeMode=0,endMode=0,endMonth=0,endDay=0,endDayOfWeek=0,endTime=0,endTimeMode=0],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2017,MONTH=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,MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0]
For next click I see:
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=false,startYear=0,startMode=0,startMonth=0,startDay=0,startDayOfWeek=0,startTime=0,startTimeMode=0,endMode=0,endMonth=0,endDay=0,endDayOfWeek=0,endTime=0,endTimeMode=0],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2018,MONTH=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,MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0]
If you see the logs, the calendar instance value does have the right MONTH and YEAR. But when I access it outside it returns 2 and 1. Why?
This same thing is happening when I do 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);
}
});
Does not matter which day I click in January, it gives me clicked day as 5. Here is the log:
I/CalendarFragment: ------- clicked day: 5 - java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=java.util.SimpleTimeZone[id=GMT,offset=0,dstSavings=3600000,useDaylight=false,startYear=0,startMode=0,startMonth=0,startDay=0,startDayOfWeek=0,startTime=0,startTimeMode=0,endMode=0,endMonth=0,endDay=0,endDayOfWeek=0,endTime=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,MILLISECOND=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,offset=0,dstSavings=3600000,useDaylight=false,startYear=0,startMode=0,startMonth=0,startDay=0,startDayOfWeek=0,startTime=0,startTimeMode=0,endMode=0,endMonth=0,endDay=0,endDayOfWeek=0,endTime=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,MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0]
Any idea why this is happening? Or what is causing it?
MONTH and YEAR are public static int vars. So accessed them with Calendar class.
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/6238.html
上一篇: 将日期添加到JavaScript日期