/** * Returns the earliest date that falls on the specified day-of-the-week * and is AFTER the base date. * * @param targetWeekday a code for the target day-of-the-week. * @param base the base date. * * @return the earliest date that falls on the specified day-of-the-week * and is AFTER the base date. */ public static SerialDate getFollowingDayOfWeek(final int targetWeekday, final SerialDate base) { // check arguments... if (!SerialDate.isValidWeekdayCode(targetWeekday)) { throw new IllegalArgumentException( "Invalid day-of-the-week code." ); } // find the date... final int adjust; final int baseDOW = base.getDayOfWeek(); if (baseDOW > targetWeekday) { adjust = 7 + Math.min(0, targetWeekday - baseDOW); } else { adjust = Math.max(0, targetWeekday - baseDOW); } return SerialDate.addDays(adjust, base); }
/** * Returns the latest date that falls on the specified day-of-the-week and * is BEFORE the base date. * * @param targetWeekday a code for the target day-of-the-week. * @param base the base date. * * @return the latest date that falls on the specified day-of-the-week and * is BEFORE the base date. */ public static SerialDate getPreviousDayOfWeek(final int targetWeekday, final SerialDate base) { // check arguments... if (!SerialDate.isValidWeekdayCode(targetWeekday)) { throw new IllegalArgumentException( "Invalid day-of-the-week code." ); } // find the date... final int adjust; final int baseDOW = base.getDayOfWeek(); if (baseDOW > targetWeekday) { adjust = Math.min(0, targetWeekday - baseDOW); } else { adjust = -7 + Math.max(0, targetWeekday - baseDOW); } return SerialDate.addDays(adjust, base); }
/** * Returns the earliest date that falls on the specified day-of-the-week * and is AFTER the base date. * * @param targetWeekday a code for the target day-of-the-week. * @param base the base date. * * @return the earliest date that falls on the specified day-of-the-week * and is AFTER the base date. */ public static SerialDate getFollowingDayOfWeek(final int targetWeekday, final SerialDate base) { // check arguments... if (!SerialDate.isValidWeekdayCode(targetWeekday)) { throw new IllegalArgumentException( "Invalid day-of-the-week code." ); } // find the date... final int adjust; final int baseDOW = base.getDayOfWeek(); if (baseDOW > targetWeekday) { adjust = 7 + Math.min(0, targetWeekday - baseDOW); } else { adjust = Math.max(0, targetWeekday - baseDOW); } return SerialDate.addDays(adjust, base); }
/** * Returns the latest date that falls on the specified day-of-the-week and * is BEFORE the base date. * * @param targetWeekday a code for the target day-of-the-week. * @param base the base date. * * @return the latest date that falls on the specified day-of-the-week and * is BEFORE the base date. */ public static SerialDate getPreviousDayOfWeek(final int targetWeekday, final SerialDate base) { // check arguments... if (!SerialDate.isValidWeekdayCode(targetWeekday)) { throw new IllegalArgumentException( "Invalid day-of-the-week code." ); } // find the date... final int adjust; final int baseDOW = base.getDayOfWeek(); if (baseDOW > targetWeekday) { adjust = Math.min(0, targetWeekday - baseDOW); } else { adjust = -7 + Math.max(0, targetWeekday - baseDOW); } return SerialDate.addDays(adjust, base); }
/** * Returns the earliest date that falls on the specified day-of-the-week * and is AFTER the base date. * * @param targetWeekday a code for the target day-of-the-week. * @param base the base date. * * @return the earliest date that falls on the specified day-of-the-week * and is AFTER the base date. */ public static SerialDate getFollowingDayOfWeek(final int targetWeekday, final SerialDate base) { // check arguments... if (!SerialDate.isValidWeekdayCode(targetWeekday)) { throw new IllegalArgumentException( "Invalid day-of-the-week code." ); } // find the date... final int adjust; final int baseDOW = base.getDayOfWeek(); if (baseDOW > targetWeekday) { adjust = 7 + Math.min(0, targetWeekday - baseDOW); } else { adjust = Math.max(0, targetWeekday - baseDOW); } return SerialDate.addDays(adjust, base); }
/** * Update the button labels and colors to reflect date selection. */ protected void refreshButtons() { SerialDate current = getFirstVisibleDate(); for (int i = 0; i < 42; i++) { final JButton button = this.buttons[i]; button.setText(String.valueOf(current.getDayOfWeek())); button.setBackground(getButtonColor(current)); current = SerialDate.addDays(1, current); } }
/** * Returns the date that falls on the specified day-of-the-week and is * CLOSEST to the base date. * * @param targetDOW a code for the target day-of-the-week. * @param base the base date. * * @return the date that falls on the specified day-of-the-week and is * CLOSEST to the base date. */ public static SerialDate getNearestDayOfWeek(final int targetDOW, final SerialDate base) { // check arguments... if (!SerialDate.isValidWeekdayCode(targetDOW)) { throw new IllegalArgumentException( "Invalid day-of-the-week code." ); } // find the date... final int baseDOW = base.getDayOfWeek(); int adjust = -Math.abs(targetDOW - baseDOW); if (adjust >= 4) { adjust = 7 - adjust; } if (adjust <= -4) { adjust = 7 + adjust; } return SerialDate.addDays(adjust, base); }
/** * Returns the latest date that falls on the specified day-of-the-week and * is BEFORE the base date. * * @param targetWeekday a code for the target day-of-the-week. * @param base the base date. * * @return the latest date that falls on the specified day-of-the-week and * is BEFORE the base date. */ public static SerialDate getPreviousDayOfWeek(final int targetWeekday, final SerialDate base) { // check arguments... if (!SerialDate.isValidWeekdayCode(targetWeekday)) { throw new IllegalArgumentException( "Invalid day-of-the-week code." ); } // find the date... final int adjust; final int baseDOW = base.getDayOfWeek(); if (baseDOW > targetWeekday) { adjust = Math.min(0, targetWeekday - baseDOW); } else { adjust = -7 + Math.max(0, targetWeekday - baseDOW); } return SerialDate.addDays(adjust, base); }
/** * Returns the date that falls on the specified day-of-the-week and is * CLOSEST to the base date. * * @param targetDOW a code for the target day-of-the-week. * @param base the base date. * * @return the date that falls on the specified day-of-the-week and is * CLOSEST to the base date. */ public static SerialDate getNearestDayOfWeek(final int targetDOW, final SerialDate base) { // check arguments... if (!SerialDate.isValidWeekdayCode(targetDOW)) { throw new IllegalArgumentException( "Invalid day-of-the-week code." ); } // find the date... final int baseDOW = base.getDayOfWeek(); int adjust = -Math.abs(targetDOW - baseDOW); if (adjust >= 4) { adjust = 7 - adjust; } if (adjust <= -4) { adjust = 7 + adjust; } return SerialDate.addDays(adjust, base); }
/** * Update the button labels and colors to reflect date selection. */ protected void refreshButtons() { SerialDate current = getFirstVisibleDate(); for (int i = 0; i < 42; i++) { final JButton button = this.buttons[i]; button.setText(String.valueOf(current.getDayOfWeek())); button.setBackground(getButtonColor(current)); current = SerialDate.addDays(1, current); } }
/** * Update the button labels and colors to reflect date selection. */ protected void refreshButtons() { SerialDate current = getFirstVisibleDate(); for (int i = 0; i < 42; i++) { final JButton button = this.buttons[i]; button.setText(String.valueOf(current.getDayOfWeek())); button.setBackground(getButtonColor(current)); current = SerialDate.addDays(1, current); } }
/** * Returns the date that falls on the specified day-of-the-week and is * CLOSEST to the base date. * * @param targetDOW a code for the target day-of-the-week. * @param base the base date. * * @return the date that falls on the specified day-of-the-week and is * CLOSEST to the base date. */ public static SerialDate getNearestDayOfWeek(final int targetDOW, final SerialDate base) { // check arguments... if (!SerialDate.isValidWeekdayCode(targetDOW)) { throw new IllegalArgumentException( "Invalid day-of-the-week code." ); } // find the date... final int baseDOW = base.getDayOfWeek(); int adjust = -Math.abs(targetDOW - baseDOW); if (adjust >= 4) { adjust = 7 - adjust; } if (adjust <= -4) { adjust = 7 + adjust; } return SerialDate.addDays(adjust, base); }
/** * Returns the first date that is visible in the grid. This should always be in the month * preceding the month of the selected date. * * @return the first visible date. */ protected SerialDate getFirstVisibleDate() { SerialDate result = SerialDate.createInstance(1, this.date.getMonth(), this.date.getYYYY()); result = SerialDate.addDays(-1, result); while (result.getDayOfWeek() != getFirstDayOfWeek()) { result = SerialDate.addDays(-1, result); } return result; }
/** * Return the date for this rule, given the year. * * @param year the year. * * @return the date generated by the rule for the given year. */ public SerialDate getDate(final int year) { SerialDate result; if (this.count != SerialDate.LAST_WEEK_IN_MONTH) { // start at the beginning of the month result = SerialDate.createInstance(1, this.month, year); while (result.getDayOfWeek() != this.dayOfWeek) { result = SerialDate.addDays(1, result); } result = SerialDate.addDays(7 * (this.count - 1), result); } else { // start at the end of the month and work backwards... result = SerialDate.createInstance(1, this.month, year); result = result.getEndOfCurrentMonth(result); while (result.getDayOfWeek() != this.dayOfWeek) { result = SerialDate.addDays(-1, result); } } return result; }
/** * Return the date for this rule, given the year. * * @param year the year. * * @return the date generated by the rule for the given year. */ public SerialDate getDate(final int year) { SerialDate result; if (this.count != SerialDate.LAST_WEEK_IN_MONTH) { // start at the beginning of the month result = SerialDate.createInstance(1, this.month, year); while (result.getDayOfWeek() != this.dayOfWeek) { result = SerialDate.addDays(1, result); } result = SerialDate.addDays(7 * (this.count - 1), result); } else { // start at the end of the month and work backwards... result = SerialDate.createInstance(1, this.month, year); result = result.getEndOfCurrentMonth(result); while (result.getDayOfWeek() != this.dayOfWeek) { result = SerialDate.addDays(-1, result); } } return result; }
/** * Return the date for this rule, given the year. * * @param year the year. * * @return the date generated by the rule for the given year. */ public SerialDate getDate(final int year) { SerialDate result; if (this.count != SerialDate.LAST_WEEK_IN_MONTH) { // start at the beginning of the month result = SerialDate.createInstance(1, this.month, year); while (result.getDayOfWeek() != this.dayOfWeek) { result = SerialDate.addDays(1, result); } result = SerialDate.addDays(7 * (this.count - 1), result); } else { // start at the end of the month and work backwards... result = SerialDate.createInstance(1, this.month, year); result = result.getEndOfCurrentMonth(result); while (result.getDayOfWeek() != this.dayOfWeek) { result = SerialDate.addDays(-1, result); } } return result; }
/** * Returns the first date that is visible in the grid. This should always be in the month * preceding the month of the selected date. * * @return the first visible date. */ protected SerialDate getFirstVisibleDate() { SerialDate result = SerialDate.createInstance(1, this.date.getMonth(), this.date.getYYYY()); result = SerialDate.addDays(-1, result); while (result.getDayOfWeek() != getFirstDayOfWeek()) { result = SerialDate.addDays(-1, result); } return result; }
/** * Returns the first date that is visible in the grid. This should always be in the month * preceding the month of the selected date. * * @return the first visible date. */ protected SerialDate getFirstVisibleDate() { SerialDate result = SerialDate.createInstance(1, this.date.getMonth(), this.date.getYYYY()); result = SerialDate.addDays(-1, result); while (result.getDayOfWeek() != getFirstDayOfWeek()) { result = SerialDate.addDays(-1, result); } return result; }
final int i = Integer.parseInt(b.getName()); final SerialDate first = getFirstVisibleDate(); final SerialDate selected = SerialDate.addDays(i, first); setDate(selected);
final int i = Integer.parseInt(b.getName()); final SerialDate first = getFirstVisibleDate(); final SerialDate selected = SerialDate.addDays(i, first); setDate(selected);