Java Code Examples for java.util.Calendar.getFirstDayOfWeek()

The following are Jave code examples for showing how to use getFirstDayOfWeek() of the java.util.Calendar class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: sunbird-lms-mw   File: BaseMetricsActor.java   View Source Code Vote up 6 votes
protected static Map<String, Object> getStartAndEndDateForWeek(String period) {
  Map<String, Object> dateMap = new HashMap<>();
  Map<String, Integer> periodMap = getDaysByPeriodStr(period);
  Calendar calendar = Calendar.getInstance();
  calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
  int firstDayOfWeek = calendar.getFirstDayOfWeek();        
  calendar.add(Calendar.DATE, -(calendar.get(Calendar.DAY_OF_WEEK)-firstDayOfWeek));
  calendar.add(Calendar.WEEK_OF_YEAR, 1);
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
  String endDateStr = sdf.format(calendar.getTime());
  dateMap.put(endDate, endDateStr);
  dateMap.put(endTimeMilis, calendar.getTimeInMillis());
  calendar.add(periodMap.get(KEY), -(periodMap.get(VALUE)));
  calendar.add(Calendar.DATE, 1);
  calendar.set(Calendar.HOUR_OF_DAY,0);
  calendar.set(Calendar.MINUTE, 0);
  calendar.set(Calendar.SECOND, 0);
  calendar.set(Calendar.MILLISECOND, 0);
  dateMap.put(INTERVAL, "1w");
  dateMap.put(FORMAT, "yyyy-ww");
  String startDateStr = sdf.format(calendar.getTime());
  dateMap.put(startDate, startDateStr);
  dateMap.put(startTimeMilis, calendar.getTimeInMillis());
  return dateMap;
}
 
Example 2
Project: SmartChart   File: CommonUtils.java   View Source Code Vote up 6 votes
public static ArrayList<Calendar> getCurrentMonthCalendar(Calendar c, int offset) {
    ArrayList<Calendar> calendars = new ArrayList<>();
    Calendar currentMonth = (Calendar) c.clone();
    currentMonth.add(Calendar.MONTH, offset);


    Calendar cal = (Calendar) c.clone();
    cal.add(Calendar.MONTH, offset);
    cal.set(Calendar.DAY_OF_MONTH, 1);
    int firstDayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
    int off = cal.getFirstDayOfWeek() - firstDayOfWeek;
    if (off > 0) {
        off -= 7;
    }
    cal.add(Calendar.DATE, off);

    while ((cal.get(MONTH) < currentMonth.get(Calendar.MONTH) + 1 || cal.get(YEAR) < currentMonth.get(Calendar.YEAR)) //
            && cal.get(YEAR) <= currentMonth.get(Calendar.YEAR)) {
        for (int i = 0; i < 7; i++) {
            calendars.add((Calendar) cal.clone());
            cal.add(Calendar.DATE, 1);
            Log.i("aaa", cal.get(Calendar.MONTH) + "--" + cal.get(Calendar.DAY_OF_MONTH));
        }
    }
    return calendars;
}
 
Example 3
Project: jdk8u-jdk   File: Bug4527203.java   View Source Code Vote up 6 votes
public static void main(String[] args) {
    Calendar huCalendar = Calendar.getInstance(new Locale("hu","HU"));
    int hufirstDayOfWeek = huCalendar.getFirstDayOfWeek();
    if (hufirstDayOfWeek != Calendar.MONDAY) {
        throw new RuntimeException();
    }

    Calendar ukCalendar = Calendar.getInstance(new Locale("uk","UA"));
    int ukfirstDayOfWeek = ukCalendar.getFirstDayOfWeek();
    if (ukfirstDayOfWeek != Calendar.MONDAY) {
        throw new RuntimeException();
    }

}
 
Example 4
Project: School1-Android   File: DashboardAdapter.java   View Source Code Vote up 5 votes
public DashboardAdapter(Context context) {
    Calendar calendar = Calendar.getInstance();
    int today = calendar.get(Calendar.DAY_OF_WEEK);

    // Today is default
    rowItems.add(new Section(context.getString(R.string.section_title_today), getSectionDate(calendar)));
    calendar.add(Calendar.DAY_OF_YEAR, 1);

    if (today >= Calendar.MONDAY && today <= Calendar.WEDNESDAY) {
        rowItems.add(new Section(context.getString(R.string.section_title_tomorrow), getSectionDate(calendar)));
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        rowItems.add(new Section(context.getString(R.string.section_title_this_week), getSectionDate(calendar)));
    } else if (today == Calendar.THURSDAY) {
        rowItems.add(new Section(context.getString(R.string.section_title_tomorrow), getSectionDate(calendar)));
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        rowItems.add(new Section(context.getString(R.string.section_title_weekend), getSectionDate(calendar)));
    } else if (today == Calendar.FRIDAY)
        rowItems.add(new Section(context.getString(R.string.section_title_weekend), getSectionDate(calendar)));
    else if (today == Calendar.SATURDAY)
        rowItems.add(new Section(context.getString(R.string.section_title_tomorrow), getSectionDate(calendar)));

    calendar = Calendar.getInstance();
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

    if (calendar.getFirstDayOfWeek() != Calendar.SUNDAY || today != Calendar.SUNDAY)
        calendar.add(Calendar.WEEK_OF_YEAR, 1);

    rowItems.add(new Section(context.getString(R.string.section_title_next_week), getSectionDate(calendar)));

    // TODO: Holiday and interval sections (between two holidays, to give a clearer overview over whats happening in the long run)
    calendar.add(Calendar.WEEK_OF_YEAR, 1);
    rowItems.add(new Section("Until the end of the universe", getSectionDate(calendar)));

    // TODO: Listen on login
    UserInfo userInfo = FirebaseAuth.getInstance().getCurrentUser();
    if (userInfo != null) {
        DocumentReference userReference = FirebaseFirestore.getInstance()
                .collection("users")
                .document(userInfo.getUid());

        userReference.collection("groups")
                .addSnapshotListener(this);
        userReference.collection("items")
                .addSnapshotListener(new GroupItemsChangeListener(FirebaseAuth.getInstance().getCurrentUser().getUid()));
    }
}
 
Example 5
Project: openjdk-jdk10   File: Bug7042647Test.java   View Source Code Vote up 5 votes
@Test
public void test() throws DatatypeConfigurationException {
    XMLGregorianCalendar xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(1970, 1, 1, 0, 0, 0, 0, 0);
    GregorianCalendar calendar = xmlCalendar.toGregorianCalendar();
    int firstDayOfWeek = calendar.getFirstDayOfWeek();
    Calendar defaultCalendar = Calendar.getInstance();
    int defaultFirstDayOfWeek = defaultCalendar.getFirstDayOfWeek();
    if (firstDayOfWeek != defaultFirstDayOfWeek) {
        Assert.fail("Failed firstDayOfWeek=" + firstDayOfWeek + " != defaultFirstDayOfWeek=" + defaultFirstDayOfWeek);
    } else {
        System.out.println("Success firstDayOfWeek=" + firstDayOfWeek + " == defaultFirstDayOfWeek=" + defaultFirstDayOfWeek);
    }
}
 
Example 6
Project: xbin-store   File: DateUtils.java   View Source Code Vote up 5 votes
/**
 * 得到当前周截止时间
 *
 * @param date
 * @return
 */
public static Date getWeekEnd(Date date) {
	Calendar calendar = Calendar.getInstance();
	calendar.setTime(date);
	calendar.get(Calendar.WEEK_OF_YEAR);
	int firstDay = calendar.getFirstDayOfWeek();
	calendar.set(Calendar.DAY_OF_WEEK, 8 - firstDay);
	calendar.set(Calendar.HOUR_OF_DAY, 23);
	calendar.set(Calendar.MINUTE, 59);
	calendar.set(Calendar.SECOND, 59);
	calendar.set(Calendar.MILLISECOND, 0);
	return calendar.getTime();
}
 
Example 7
Project: ureport   File: WeekFunction.java   View Source Code Vote up 5 votes
@Override
public Object execute(List<ExpressionData<?>> dataList, Context context,Cell currentCell) {
	Calendar c = buildCalendar(dataList);
	int weekDay=c.get(Calendar.DAY_OF_WEEK);
	boolean isFirstSunday = (c.getFirstDayOfWeek() == Calendar.SUNDAY);
	if(isFirstSunday){
		weekDay = weekDay - 1;
		if(weekDay == 0){
			weekDay = 7;
		}
	}
	switch(weekDay){
	case 1:
		return "星期一";
	case 2:
		return "星期二";
	case 3:
		return "星期三";
	case 4:
		return "星期四";
	case 5:
		return "星期五";
	case 6:
		return "星期六";
	case 7:
		return "星期日";
	}
	throw new ReportComputeException("Unknow week day :"+weekDay);
}
 
Example 8
Project: mumu   File: DateUtils.java   View Source Code Vote up 5 votes
/**
 * 得到当前周截止时间
 * 
 * @param date
 * @return
 */
public static Date getWeekEnd(Date date) {
	Calendar calendar = Calendar.getInstance();
	calendar.setTime(date);
	calendar.get(Calendar.WEEK_OF_YEAR);
	int firstDay = calendar.getFirstDayOfWeek();
	calendar.set(Calendar.DAY_OF_WEEK, 8 - firstDay);
	calendar.set(Calendar.HOUR_OF_DAY, 23);
	calendar.set(Calendar.MINUTE, 59);
	calendar.set(Calendar.SECOND, 59);
	calendar.set(Calendar.MILLISECOND, 0);
	return calendar.getTime();
}
 
Example 9
Project: openjdk-jdk10   File: LocaleTest.java   View Source Code Vote up 5 votes
/**
 * @bug 4143951
 * Russian first day of week should be Monday. Confirmed.
 */
public void Test4143951() {
    Calendar cal = Calendar.getInstance(new Locale("ru", "", ""));
    if (cal.getFirstDayOfWeek() != Calendar.MONDAY) {
        errln("Fail: First day of week in Russia should be Monday");
    }
}
 
Example 10
Project: DMS   File: CalendarUtility.java   View Source Code Vote up 5 votes
/**
 *
 * @param weekIndex
 * @param calendar
 * @return
 */
public static int getWeekIndex(int weekIndex, Calendar calendar) {
    int firstDayWeekPosition = calendar.getFirstDayOfWeek();
    if (firstDayWeekPosition == 1) {
        return weekIndex;
    } else {
        if (weekIndex == 1) {
            return 7;
        } else {
            return weekIndex - 1;
        }
    }
}
 
Example 11
Project: DateRangePickerLibrary   File: MonthView.java   View Source Code Vote up 5 votes
public static MonthView create(ViewGroup parent, LayoutInflater inflater,
                               DateFormat weekdayNameFormat, Listener listener, Calendar today, int dividerColor,
                               int dayBackgroundResId, int dayTextColorResId, int titleTextColor, boolean displayHeader,
                               int headerTextColor, List<CalendarCellDecorator> decorators, Locale locale,
                               DayViewAdapter adapter) {
  final MonthView view = (MonthView) inflater.inflate(R.layout.month, parent, false);
  view.setDayViewAdapter(adapter);
  view.setDividerColor(dividerColor);
  view.setDayTextColor(dayTextColorResId);
  view.setTitleTextColor(titleTextColor);
  view.setDisplayHeader(displayHeader);
  view.setHeaderTextColor(headerTextColor);

  if (dayBackgroundResId != 0) {
    view.setDayBackground(dayBackgroundResId);
  }

  final int originalDayOfWeek = today.get(Calendar.DAY_OF_WEEK);

  view.isRtl = isRtl(locale);
  view.locale = locale;
  int firstDayOfWeek = today.getFirstDayOfWeek();
  final CalendarRowView headerRow = (CalendarRowView) view.grid.getChildAt(0);
  for (int offset = 0; offset < 7; offset++) {
    today.set(Calendar.DAY_OF_WEEK, getDayOfWeek(firstDayOfWeek, offset, view.isRtl));
    final TextView textView = (TextView) headerRow.getChildAt(offset);
    textView.setText(weekdayNameFormat.format(today.getTime()));
  }
  today.set(Calendar.DAY_OF_WEEK, originalDayOfWeek);
  view.listener = listener;
  view.decorators = decorators;
  return view;
}
 
Example 12
Project: OpenJSharp   File: CalendarBuilder.java   View Source Code Vote up 5 votes
Calendar establish(Calendar cal) {
    boolean weekDate = isSet(WEEK_YEAR)
                        && field[WEEK_YEAR] > field[YEAR];
    if (weekDate && !cal.isWeekDateSupported()) {
        // Use YEAR instead
        if (!isSet(YEAR)) {
            set(YEAR, field[MAX_FIELD + WEEK_YEAR]);
        }
        weekDate = false;
    }

    cal.clear();
    // Set the fields from the min stamp to the max stamp so that
    // the field resolution works in the Calendar.
    for (int stamp = MINIMUM_USER_STAMP; stamp < nextStamp; stamp++) {
        for (int index = 0; index <= maxFieldIndex; index++) {
            if (field[index] == stamp) {
                cal.set(index, field[MAX_FIELD + index]);
                break;
            }
        }
    }

    if (weekDate) {
        int weekOfYear = isSet(WEEK_OF_YEAR) ? field[MAX_FIELD + WEEK_OF_YEAR] : 1;
        int dayOfWeek = isSet(DAY_OF_WEEK) ?
                            field[MAX_FIELD + DAY_OF_WEEK] : cal.getFirstDayOfWeek();
        if (!isValidDayOfWeek(dayOfWeek) && cal.isLenient()) {
            if (dayOfWeek >= 8) {
                dayOfWeek--;
                weekOfYear += dayOfWeek / 7;
                dayOfWeek = (dayOfWeek % 7) + 1;
            } else {
                while (dayOfWeek <= 0) {
                    dayOfWeek += 7;
                    weekOfYear--;
                }
            }
            dayOfWeek = toCalendarDayOfWeek(dayOfWeek);
        }
        cal.setWeekDate(field[MAX_FIELD + WEEK_YEAR], weekOfYear, dayOfWeek);
    }
    return cal;
}
 
Example 13
Project: jdk8u-jdk   File: CalendarBuilder.java   View Source Code Vote up 5 votes
Calendar establish(Calendar cal) {
    boolean weekDate = isSet(WEEK_YEAR)
                        && field[WEEK_YEAR] > field[YEAR];
    if (weekDate && !cal.isWeekDateSupported()) {
        // Use YEAR instead
        if (!isSet(YEAR)) {
            set(YEAR, field[MAX_FIELD + WEEK_YEAR]);
        }
        weekDate = false;
    }

    cal.clear();
    // Set the fields from the min stamp to the max stamp so that
    // the field resolution works in the Calendar.
    for (int stamp = MINIMUM_USER_STAMP; stamp < nextStamp; stamp++) {
        for (int index = 0; index <= maxFieldIndex; index++) {
            if (field[index] == stamp) {
                cal.set(index, field[MAX_FIELD + index]);
                break;
            }
        }
    }

    if (weekDate) {
        int weekOfYear = isSet(WEEK_OF_YEAR) ? field[MAX_FIELD + WEEK_OF_YEAR] : 1;
        int dayOfWeek = isSet(DAY_OF_WEEK) ?
                            field[MAX_FIELD + DAY_OF_WEEK] : cal.getFirstDayOfWeek();
        if (!isValidDayOfWeek(dayOfWeek) && cal.isLenient()) {
            if (dayOfWeek >= 8) {
                dayOfWeek--;
                weekOfYear += dayOfWeek / 7;
                dayOfWeek = (dayOfWeek % 7) + 1;
            } else {
                while (dayOfWeek <= 0) {
                    dayOfWeek += 7;
                    weekOfYear--;
                }
            }
            dayOfWeek = toCalendarDayOfWeek(dayOfWeek);
        }
        cal.setWeekDate(field[MAX_FIELD + WEEK_YEAR], weekOfYear, dayOfWeek);
    }
    return cal;
}
 
Example 14
Project: openjdk-jdk10   File: Bug4527203.java   View Source Code Vote up 5 votes
public static void main(String[] args) {
    Calendar huCalendar = Calendar.getInstance(new Locale("hu","HU"));
    int hufirstDayOfWeek = huCalendar.getFirstDayOfWeek();
    if (hufirstDayOfWeek != Calendar.MONDAY) {
        throw new RuntimeException();
    }

    Calendar ukCalendar = Calendar.getInstance(new Locale("uk","UA"));
    int ukfirstDayOfWeek = ukCalendar.getFirstDayOfWeek();
    if (ukfirstDayOfWeek != Calendar.MONDAY) {
        throw new RuntimeException();
    }

}
 
Example 15
Project: openjdk-jdk10   File: CalendarBuilder.java   View Source Code Vote up 5 votes
Calendar establish(Calendar cal) {
    boolean weekDate = isSet(WEEK_YEAR)
                        && field[WEEK_YEAR] > field[YEAR];
    if (weekDate && !cal.isWeekDateSupported()) {
        // Use YEAR instead
        if (!isSet(YEAR)) {
            set(YEAR, field[MAX_FIELD + WEEK_YEAR]);
        }
        weekDate = false;
    }

    cal.clear();
    // Set the fields from the min stamp to the max stamp so that
    // the field resolution works in the Calendar.
    for (int stamp = MINIMUM_USER_STAMP; stamp < nextStamp; stamp++) {
        for (int index = 0; index <= maxFieldIndex; index++) {
            if (field[index] == stamp) {
                cal.set(index, field[MAX_FIELD + index]);
                break;
            }
        }
    }

    if (weekDate) {
        int weekOfYear = isSet(WEEK_OF_YEAR) ? field[MAX_FIELD + WEEK_OF_YEAR] : 1;
        int dayOfWeek = isSet(DAY_OF_WEEK) ?
                            field[MAX_FIELD + DAY_OF_WEEK] : cal.getFirstDayOfWeek();
        if (!isValidDayOfWeek(dayOfWeek) && cal.isLenient()) {
            if (dayOfWeek >= 8) {
                dayOfWeek--;
                weekOfYear += dayOfWeek / 7;
                dayOfWeek = (dayOfWeek % 7) + 1;
            } else {
                while (dayOfWeek <= 0) {
                    dayOfWeek += 7;
                    weekOfYear--;
                }
            }
            dayOfWeek = toCalendarDayOfWeek(dayOfWeek);
        }
        cal.setWeekDate(field[MAX_FIELD + WEEK_YEAR], weekOfYear, dayOfWeek);
    }
    return cal;
}
 
Example 16
Project: openjdk-jdk10   File: Bug4302966.java   View Source Code Vote up 5 votes
public static void main(String[] args) {
    Calendar czechCalendar = Calendar.getInstance(new Locale("cs"));
    int firstDayOfWeek = czechCalendar.getFirstDayOfWeek();
    if (firstDayOfWeek != Calendar.MONDAY) {
        throw new RuntimeException();
    }
}
 
Example 17
Project: mumu-core   File: DateUtils.java   View Source Code Vote up 5 votes
/**
 * 得到当前周截止时间
 * 
 * @param date
 * @return
 */
public static Date getWeekEnd(Date date) {
	Calendar calendar = Calendar.getInstance();
	calendar.setTime(date);
	calendar.get(Calendar.WEEK_OF_YEAR);
	int firstDay = calendar.getFirstDayOfWeek();
	calendar.set(Calendar.DAY_OF_WEEK, 8 - firstDay);
	calendar.set(Calendar.HOUR_OF_DAY, 23);
	calendar.set(Calendar.MINUTE, 59);
	calendar.set(Calendar.SECOND, 59);
	calendar.set(Calendar.MILLISECOND, 0);
	return calendar.getTime();
}
 
Example 18
Project: DateRangePicker   File: CalendarPickerView.java   View Source Code Vote up 4 votes
List<List<MonthCellDescriptor>> getMonthCells(MonthDescriptor month, Calendar startCal) {
  Calendar cal = Calendar.getInstance(timeZone, locale);
  cal.setTime(startCal.getTime());
  List<List<MonthCellDescriptor>> cells = new ArrayList<>();
  cal.set(DAY_OF_MONTH, 1);
  int firstDayOfWeek = cal.get(DAY_OF_WEEK);
  int offset = cal.getFirstDayOfWeek() - firstDayOfWeek;
  if (offset > 0) {
    offset -= 7;
  }
  cal.add(Calendar.DATE, offset);

  Calendar minSelectedCal = minDate(selectedCals);
  Calendar maxSelectedCal = maxDate(selectedCals);

  while ((cal.get(MONTH) < month.getMonth() + 1 || cal.get(YEAR) < month.getYear()) //
      && cal.get(YEAR) <= month.getYear()) {
    Logr.d("Building week row starting at %s", cal.getTime());
    List<MonthCellDescriptor> weekCells = new ArrayList<>();
    cells.add(weekCells);
    for (int c = 0; c < 7; c++) {
      Date date = cal.getTime();
      boolean isCurrentMonth = cal.get(MONTH) == month.getMonth();
      boolean isSelected = isCurrentMonth && containsDate(selectedCals, cal);
      boolean isSelectable =
          isCurrentMonth && betweenDates(cal, minCal, maxCal) && isDateSelectable(date);
      boolean isToday = sameDate(cal, today);
      boolean isHighlighted = containsDate(highlightedCals, cal);
      int value = cal.get(DAY_OF_MONTH);

      RangeState rangeState = RangeState.NONE;
      if (selectedCals.size() > 1) {
        if (sameDate(minSelectedCal, cal)) {
          rangeState = RangeState.FIRST;
        } else if (sameDate(maxDate(selectedCals), cal)) {
          rangeState = RangeState.LAST;
        } else if (betweenDates(cal, minSelectedCal, maxSelectedCal)) {
          rangeState = RangeState.MIDDLE;
        }
      }

      weekCells.add(
          new MonthCellDescriptor(date, isCurrentMonth, isSelectable, isSelected, isToday,
              isHighlighted, value, rangeState));
      cal.add(DATE, 1);
    }
  }
  return cells;
}
 
Example 19
Project: DateRangePickerLibrary   File: CalendarPickerView.java   View Source Code Vote up 4 votes
List<List<MonthCellDescriptor>> getMonthCells(MonthDescriptor month, Calendar startCal) {
  Calendar cal = Calendar.getInstance(timeZone, locale);
  cal.setTime(startCal.getTime());
  List<List<MonthCellDescriptor>> cells = new ArrayList<>();
  cal.set(DAY_OF_MONTH, 1);
  int firstDayOfWeek = cal.get(DAY_OF_WEEK);
  int offset = cal.getFirstDayOfWeek() - firstDayOfWeek;
  if (offset > 0) {
    offset -= 7;
  }
  cal.add(Calendar.DATE, offset);

  Calendar minSelectedCal = minDate(selectedCals);
  Calendar maxSelectedCal = maxDate(selectedCals);

  while ((cal.get(MONTH) < month.getMonth() + 1 || cal.get(YEAR) < month.getYear()) //
      && cal.get(YEAR) <= month.getYear()) {
    Logr.d("Building week row starting at %s", cal.getTime());
    List<MonthCellDescriptor> weekCells = new ArrayList<>();
    cells.add(weekCells);
    for (int c = 0; c < 7; c++) {
      Date date = cal.getTime();
      boolean isCurrentMonth = cal.get(MONTH) == month.getMonth();
      boolean isSelected = isCurrentMonth && containsDate(selectedCals, cal);
      boolean isSelectable =
          isCurrentMonth && betweenDates(cal, minCal, maxCal) && isDateSelectable(date);
      boolean isToday = sameDate(cal, today);
      boolean isHighlighted = containsDate(highlightedCals, cal);
      int value = cal.get(DAY_OF_MONTH);

      RangeState rangeState = RangeState.NONE;
      if (selectedCals.size() > 1) {
        if (sameDate(minSelectedCal, cal)) {
          rangeState = RangeState.FIRST;
        } else if (sameDate(maxDate(selectedCals), cal)) {
          rangeState = RangeState.LAST;
        } else if (betweenDates(cal, minSelectedCal, maxSelectedCal)) {
          rangeState = RangeState.MIDDLE;
        }
      }

      weekCells.add(
          new MonthCellDescriptor(date, isCurrentMonth, isSelectable, isSelected, isToday,
              isHighlighted, value, rangeState));
      cal.add(DATE, 1);
    }
  }
  return cells;
}
 
Example 20
Project: graphing-loan-analyzer   File: CalendarViewSkin.java   View Source Code Vote up 4 votes
/**
 * Updates the days.
 */
private void updateDays() {
    Calendar calendar = (Calendar) calendarView.getCalendar().clone();

    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    // Set the calendar to the first day of the current month.
    calendar.set(Calendar.DAY_OF_MONTH, 1);

    int month = calendar.get(Calendar.MONTH);

    // Set the calendar to the end of the previous month.
    while (calendar.getFirstDayOfWeek() != calendar.get(Calendar.DAY_OF_WEEK)) {
        calendar.add(Calendar.DAY_OF_MONTH, -1);
    }

    boolean firstLoop = true;

    // Ignore the week day row and the week number column
    for (int i = numberOfDaysPerWeek + (calendarView.getShowWeeks() ? 1 : 0); i < getChildren().size(); i++) {

        // If we are in the first column and if we want to show the weeks, set them.
        if (i % (numberOfDaysPerWeek + 1) == 0 && calendarView.getShowWeeks()) {
            Label label = (Label) getChildren().get(i);
            label.setText(Integer.toString(calendar.get(Calendar.WEEK_OF_YEAR)));
        } else {

            DateCell dateCell = (DateCell) getChildren().get(i);

            boolean disabled = normalizedMaxDate.get() != null && calendar.getTime().after(normalizedMaxDate.get())
                    || normalizedMinDate.get() != null && calendar.getTime().before(normalizedMinDate.get());

            dateCell.setDisable(disabled);

            dateCell.updateInRange(calendar.get(Calendar.MONTH) == month);
            dateCell.setItem(calendar.getTime());
            dateCell.requestLayout();
            if (firstLoop) {
                firstLoop = false;
            }

            calendar.add(Calendar.DATE, 1);
        }
    }
}