/** * Gets the first day of the first week in the currently specified month. * * @return the first day */ public Date getCurrentFirstDayOfFirstWeek() { int wkDayOfMonth1st = currentMonth.getDay(); int start = CalendarUtil.getStartingDayOfWeek(); if (wkDayOfMonth1st == start) { // always return a copy to allow SimpleCalendarView to adjust first // display date return new Date(currentMonth.getTime()); } else { Date d = new Date(currentMonth.getTime()); int offset = wkDayOfMonth1st - start > 0 ? wkDayOfMonth1st - start : DAYS_IN_WEEK - (start - wkDayOfMonth1st); CalendarUtil.addDaysToDate(d, -offset); return d; } }
/** * Shifts the currently specified date by the given number of months. The day * of the month will be pinned to the original value as far as possible. * * @param deltaMonths - number of months to be added to the current date */ public void shiftCurrentMonth(int deltaMonths) { CalendarUtil.addMonthsToDate(currentMonth, deltaMonths); refresh(); }
private DateCell getCell(Date d) { int index = CalendarUtil.getDaysBetween(firstDisplayed, d); if (index < 0 || grid.getNumCells() <= index) { return null; } DateCell cell = (DateCell) grid.getCell(index); if (cell.getValue().getDate() != d.getDate()) { throw new IllegalStateException(d + " cannot be associated with cell " + cell + " as it has date " + cell.getValue()); } return cell; } }
private static void addDays(Date date, int days) { CalendarUtil.addDaysToDate(date, days); // We might hit DST transition. Try reseting back so follow up days continue showing midnight. CalendarUtil.resetTime(date); }
/** * Returns the number of days between the two dates. Time is ignored. * * @param start starting date * @param finish ending date * @return the different */ public static int getDaysBetween(Date start, Date finish) { // Convert the dates to the same time start = copyDate(start); resetTime(start); finish = copyDate(finish); resetTime(finish); long aTime = start.getTime(); long bTime = finish.getTime(); long adjust = 60 * 60 * 1000; adjust = (bTime > aTime) ? adjust : -adjust; return (int) ((bTime - aTime + adjust) / (24 * 60 * 60 * 1000)); }
/** * Returns a {@link DateRange} starting on first day of month in which the specified date is and ending on last day of that * month. * * @param date date from which to get the month date range * @return {@link DateRange} representing the month in which the specified date is */ @SuppressWarnings("deprecation") public static DateRange getMonthDateRange(Date date) { Date startDate = new Date(date.getTime()); CalendarUtil.setToFirstDayOfMonth(startDate); // the above method will set hours to 12 startDate.setHours(0); Date endDate = new Date(date.getTime()); CalendarUtil.setToFirstDayOfMonth(endDate); CalendarUtil.addMonthsToDate(endDate, 1); CalendarUtil.addDaysToDate(endDate, -1); endDate.setHours(0); return new DateRange(startDate, endDate, CalendarUtil.getDaysBetween(startDate, endDate)); }
@SuppressWarnings("deprecation") public static Date getSameOrClosestDateInNextMonth(Date date) { Date desiredDate = new Date(date.getTime()); CalendarUtil.addMonthsToDate(desiredDate, 1); if (desiredDate.getMonth() > date.getMonth() + 1) { // skipped one month, e.g. 30 January -> 2nd (or 1st for leap-year) March, because February does not have 30th // set the date to last day of previous month CalendarUtil.setToFirstDayOfMonth(desiredDate); CalendarUtil.addDaysToDate(desiredDate, -1); } return desiredDate; }
switch (keyCode) { case KeyCodes.KEY_LEFT: CalendarUtil.addDaysToDate(this.cursor, -1); handleKey = true; break; case KeyCodes.KEY_RIGHT: CalendarUtil.addDaysToDate(this.cursor, 1); handleKey = true; break; case KeyCodes.KEY_UP: CalendarUtil.addDaysToDate(this.cursor, -7); handleKey = true; break; case KeyCodes.KEY_DOWN: CalendarUtil.addDaysToDate(this.cursor, 7); handleKey = true; break; case KeyCodes.KEY_PAGEUP: CalendarUtil.addMonthsToDate(this.cursor, -1); handleKey = true; break; case KeyCodes.KEY_PAGEDOWN: CalendarUtil.addMonthsToDate(this.cursor, 1); handleKey = true; break;
@SuppressWarnings("deprecation") private static DateRange getDateRangeStartingOnMonday(Date dateWithinTheWeek, int nrOfDaysTotal) { Date startDate = new Date(dateWithinTheWeek.getTime()); int day = startDate.getDay() - 1; int daysAfterMonday = day; if (day == -1) { // corner case when the date within the week in Sunday and thus getDay() == 0 (and day == -1), so we need Monday // from that week, which is 6 days back daysAfterMonday = 6; } CalendarUtil.addDaysToDate(startDate, -daysAfterMonday); Date endDate = new Date(startDate.getTime()); CalendarUtil.addDaysToDate(endDate, nrOfDaysTotal - 1); return new DateRange(startDate, endDate, CalendarUtil.getDaysBetween(startDate, endDate)); }
/** * Gets the highlighted date (the one the mouse is hovering over), if any. * * @return the highlighted date */ public final Date getHighlightedDate() { return CalendarUtil.copyDate(highlighted); }
protected Date getPreviousDate(Date date) { Date prev = new Date(date.getTime()); CalendarUtil.addDaysToDate(prev, -1); return prev; } }
int shift = CalendarUtil.getStartingDayOfWeek(); int dayIdx = i + shift < CalendarModel.DAYS_IN_WEEK ? i + shift : i + shift - CalendarModel.DAYS_IN_WEEK; grid.setText(0, i, getModel().formatDayOfWeek(dayIdx)); if (CalendarUtil.isWeekend(dayIdx)) { formatter.setStyleName(0, i, css().weekendLabel()); if (weekendStartColumn == -1) {
/** * Constructor. */ public CalendarModel() { currentMonth = new Date(); CalendarUtil.setToFirstDayOfMonth(currentMonth); // Finding day of week names Date date = new Date(); for (int i = 1; i <= DAYS_IN_WEEK; i++) { date.setDate(i); int dayOfWeek = date.getDay(); dayOfWeekNames[dayOfWeek] = getDayOfWeekFormatter().format(date); } // Finding day of month names date.setMonth(0); for (int i = 1; i < MAX_DAYS_IN_MONTH; ++i) { date.setDate(i); dayOfMonthNames[i] = getDayOfMonthFormatter().format(date); } // finding month names date.setDate(1); for (int i = 0; i < MONTHS_IN_YEAR; ++i) { date.setMonth(i); monthOfYearNames[i] = getMonthFormatter().format(date); } }
/** * Sets a date object to be at the beginning of the month and no time * specified. * * @param date the date */ @SuppressWarnings("deprecation") // GWT requires Date public static void setToFirstDayOfMonth(Date date) { resetTime(date); date.setDate(1); }
/** * Resets the date to have no time modifiers. Note that the hour might not be zero if the time * hits a DST transition date. * * @param date the date */ @SuppressWarnings("deprecation") // GWT requires Date public static void resetTime(Date date) { long msec = resetMilliseconds(date.getTime()); date.setTime(msec); date.setHours(0); date.setMinutes(0); date.setSeconds(0); }
/** * Is the date currently shown in the date picker? * * @param date * * @return is the date currently shown */ public boolean isDateVisible(Date date) { CalendarView r = getView(); Date first = r.getFirstDate(); Date last = r.getLastDate(); return (date != null && (CalendarUtil.isSameDate(first, date) || CalendarUtil.isSameDate(last, date) || (first.before(date) && last.after(date)))); }
private Date lastDayOfMonth(Date month) { Date lastDay = (Date) month.clone(); CalendarUtil.addMonthsToDate(lastDay, 1); CalendarUtil.setToFirstDayOfMonth(lastDay); CalendarUtil.addDaysToDate(lastDay, -1); return justDay(lastDay); }
switch (keyCode) { case KeyCodes.KEY_LEFT: CalendarUtil.addDaysToDate(this.cursor, -1); handleKey = true; break; case KeyCodes.KEY_RIGHT: CalendarUtil.addDaysToDate(this.cursor, 1); handleKey = true; break; case KeyCodes.KEY_UP: CalendarUtil.addDaysToDate(this.cursor, -7); handleKey = true; break; case KeyCodes.KEY_DOWN: CalendarUtil.addDaysToDate(this.cursor, 7); handleKey = true; break; case KeyCodes.KEY_PAGEUP: CalendarUtil.addMonthsToDate(this.cursor, -1); handleKey = true; break; case KeyCodes.KEY_PAGEDOWN: CalendarUtil.addMonthsToDate(this.cursor, 1); handleKey = true; break;
/** * Returns the selected date, or null if none is selected. * * @return the selected date, or null */ public final Date getValue() { return CalendarUtil.copyDate(value); }
case DAY: if ( back ) { CalendarUtil.addDaysToDate( currentDate, -1 ); } else { CalendarUtil.addDaysToDate( currentDate, 1 ); CalendarUtil.addDaysToDate( currentDate, -7 ); } else { CalendarUtil.addDaysToDate( currentDate, 7 );