/**
   * Select the dates from fromDate to toDate. By default the background color is holo_blue_light,
   * and the text color is black. You can customize the background by changing
   * CaldroidFragment.selectedBackgroundDrawable, and change the text color
   * CaldroidFragment.selectedTextColor before call this method. This method does not refresh view,
   * need to call refreshView()
   *
   * @param fromDate
   * @param toDate
   */
  public void setSelectedDates(Date fromDate, Date toDate) {
    // Ensure fromDate is before toDate
    if (fromDate == null || toDate == null || fromDate.after(toDate)) {
      return;
    }

    selectedDates.clear();

    DateTime fromDateTime = CalendarHelper.convertDateToDateTime(fromDate);
    DateTime toDateTime = CalendarHelper.convertDateToDateTime(toDate);

    DateTime dateTime = fromDateTime;
    while (dateTime.lt(toDateTime)) {
      selectedDates.add(dateTime);
      dateTime = dateTime.plusDays(1);
    }
    selectedDates.add(toDateTime);
  }
  /**
   * Move calendar to specified dateTime, with animation
   *
   * @param dateTime
   */
  public void moveToDateTime(DateTime dateTime) {

    DateTime firstOfMonth = new DateTime(year, month, 1, 0, 0, 0, 0);
    DateTime lastOfMonth = firstOfMonth.getEndOfMonth();

    // To create a swipe effect
    // Do nothing if the dateTime is in current month

    // Calendar swipe left when dateTime is in the past
    if (dateTime.lt(firstOfMonth)) {
      // Get next month of dateTime. When swipe left, month will
      // decrease
      DateTime firstDayNextMonth = dateTime.plus(0, 1, 0, 0, 0, 0, 0, DateTime.DayOverflow.LastDay);

      // Refresh adapters
      pageChangeListener.setCurrentDateTime(firstDayNextMonth);
      int currentItem = dateViewPager.getCurrentItem();
      pageChangeListener.refreshAdapters(currentItem);

      // Swipe left
      dateViewPager.setCurrentItem(currentItem - 1);
    }

    // Calendar swipe right when dateTime is in the future
    else if (dateTime.gt(lastOfMonth)) {
      // Get last month of dateTime. When swipe right, the month will
      // increase
      DateTime firstDayLastMonth =
          dateTime.minus(0, 1, 0, 0, 0, 0, 0, DateTime.DayOverflow.LastDay);

      // Refresh adapters
      pageChangeListener.setCurrentDateTime(firstDayLastMonth);
      int currentItem = dateViewPager.getCurrentItem();
      pageChangeListener.refreshAdapters(currentItem);

      // Swipe right
      dateViewPager.setCurrentItem(currentItem + 1);
    }
  }
Example #3
0
  /**
   * Retrieve all the dates for a given calendar month Include previous month, current month and
   * next month.
   *
   * @param month
   * @param year
   * @param startDayOfWeek : calendar can start from customized date instead of Sunday
   * @return
   */
  public static ArrayList<DateTime> getFullWeeks(
      int month, int year, int startDayOfWeek, boolean sixWeeksInCalendar) {
    ArrayList<DateTime> datetimeList = new ArrayList<DateTime>();

    DateTime firstDateOfMonth = new DateTime(year, month, 1, 0, 0, 0, 0);
    DateTime lastDateOfMonth = firstDateOfMonth.plusDays(firstDateOfMonth.getNumDaysInMonth() - 1);

    // Add dates of first week from previous month
    int weekdayOfFirstDate = firstDateOfMonth.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 = firstDateOfMonth.minusDays(weekdayOfFirstDate - startDayOfWeek);
      if (!dateTime.lt(firstDateOfMonth)) {
        break;
      }

      datetimeList.add(dateTime);
      weekdayOfFirstDate--;
    }

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

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

    if (endDayOfWeek == 0) {
      endDayOfWeek = 7;
    }

    if (lastDateOfMonth.getWeekDay() != endDayOfWeek) {
      int i = 1;
      while (true) {
        DateTime nextDay = lastDateOfMonth.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;
  }