1

如果我想修改日历并将我的月份从 2016 年 1 月 15 日开始更改为 2016 年 2 月 16 日,如何在 calroid 日历库中更改月份的开始日期和结束日期。

4

1 回答 1

3

您好,使用 CalendarHelper 类中的以下修改,您可以设置轮班日历的开始日期。我正在使用以下库进行日历视图https://github.com/roomorama/Caldroid

请修改 getFullWeeks 方法

/** * 检索给定日历月的所有日期,包括上个月、* 当前月和下个月。* * @param month * @param year * @param startDayOfWeek : 日历可以从自定义日期开始,而不是星期日 * @return */

   public static ArrayList<DateTime> getFullWeeks(int month, int year, int startDayOfWeek,int startDayOfMonth, boolean sixWeeksInCalendar) {
            ArrayList<DateTime> datetimeList = new ArrayList<DateTime>();

            int dayCount=startDayOfMonth;

            DateTime firstDateOfMonth = new DateTime(year, month, 1, 0, 0, 0, 0);
            DateTime firstDateOfMonthToSet = new DateTime(year, month, dayCount, 0, 0, 0, 0);

            int daysToAdd=firstDateOfMonth.getNumDaysInMonth()-dayCount;

            DateTime lastDateOfMonth = firstDateOfMonthToSet.plusDays(daysToAdd);
            DateTime lastDateOfMonthTpSet =firstDateOfMonthToSet.plusDays(firstDateOfMonth.getNumDaysInMonth()-1);

            // Add dates of first week from previous month
           // int weekdayOfFirstDate = firstDateOfMonth.getWeekDay();
    //dr
            int weekdayOfFirstDate = firstDateOfMonthToSet.getWeekDay();
            // If weekdayOfFirstDate smaller than startDayOfWeek
            // For e.g: weekdayFirstDate is Monday, startDayOfWeek is Tuesday
            // increase the weekday of FirstDate because it's in the future
            if (weekdayOfFirstDate < startDayOfWeek) {
                weekdayOfFirstDate += 7;
            }

            while (weekdayOfFirstDate > 0) {
                DateTime dateTime = firstDateOfMonthToSet.minusDays(weekdayOfFirstDate
                        - startDayOfWeek);
                if (!dateTime.lt(firstDateOfMonthToSet)) {
                    break;
                }
                datetimeList.add(dateTime);
                weekdayOfFirstDate--;
            }

            // Add dates of current month
            for (int i = 0; i < lastDateOfMonth.getDay(); i++) {
                datetimeList.add(firstDateOfMonthToSet.plusDays(i));
            }

            // Add dates of last week from next month
            int endDayOfWeek = startDayOfWeek - 1;//dr
           // int endDayOfWeek = startDayOfWeek;

            if (endDayOfWeek == 0) {
                endDayOfWeek = 7;
            }
            if (lastDateOfMonthTpSet.getWeekDay() != endDayOfWeek) {
                int i = 1;
                while (true) {
                    DateTime nextDay = lastDateOfMonthTpSet.plusDays(i);
                    datetimeList.add(nextDay);
                    i++;
                    if (nextDay.getWeekDay() == endDayOfWeek) {
                        break;
                    }
                }
            }
            // Add more weeks to fill remaining rows
            if (sixWeeksInCalendar) {
                int size = datetimeList.size();
                int row = size / 7;
                int numOfDays = (6 - row) * 7;
                DateTime lastDateTime = datetimeList.get(size - 1);
                for (int i = 1; i <= numOfDays; i++) {
                    DateTime nextDateTime = lastDateTime.plusDays(i);
                    datetimeList.add(nextDateTime);
                }
            }
            return datetimeList;
        }

希望对你有帮助 :)

于 2016-10-13T09:19:36.183 回答