日历在 MaterialCalendarView 中返回不正确的 MONTH、YEAR 和 DAY_OF_MONTH

Posted

技术标签:

【中文标题】日历在 MaterialCalendarView 中返回不正确的 MONTH、YEAR 和 DAY_OF_MONTH【英文标题】:Calendar returns incorrect MONTH, YEAR and DAY_OF_MONTH in MaterialCalendarView 【发布时间】:2018-06-16 16:56:43 【问题描述】:

我正在为我的应用日历使用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=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]

对于下一次点击,我看到了:

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=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]

如果您看到日志,则日历实例值确实具有正确的 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);

            
        );

不管我在一月的哪一天点击,它都会给我点击天数为 5。这是日志:

I/CalendarFragment: ------- 点击天数: 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: ------- 点击天数: 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]

知道为什么会这样吗?或者是什么原因造成的?

【问题讨论】:

【参考方案1】:

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

【讨论】:

以上是关于日历在 MaterialCalendarView 中返回不正确的 MONTH、YEAR 和 DAY_OF_MONTH的主要内容,如果未能解决你的问题,请参考以下文章

Android开源日历,materialcalendarview的选中背景如何缩小

Android Spans介绍(转)

android中怎么在日历中实现签到

有没有可以在日历上做标记的Android App?

2021年全年日历一张,怎么做日历简单又漂亮?

带有谷歌日历的完整日历