/** * Returns the actual number of days between two dates. * * @param start the start date. * @param end the end date. * * @return the number of days between the start date and the end date. */ public static int dayCountActual(final SerialDate start, final SerialDate end) { return end.compare(start); }
/** * Rolls the date forward to the last day of the month. * * @param base the base date. * * @return a new serial date. */ public SerialDate getEndOfCurrentMonth(final SerialDate base) { final int last = SerialDate.lastDayOfMonth( base.getMonth(), base.getYYYY() ); return SerialDate.createInstance(last, base.getMonth(), base.getYYYY()); }
/** * Converts the date to a string. * * @return a string representation of the date. */ public String toString() { return getDayOfMonth() + "-" + SerialDate.monthCodeToString(getMonth()) + "-" + getYYYY(); }
/** * Creates a new date by adding the specified number of days to the base * date. * * @param days the number of days to add (can be negative). * @param base the base date. * * @return a new date. */ public static SerialDate addDays(final int days, final SerialDate base) { final int serialDayNumber = base.toSerial() + days; return SerialDate.createInstance(serialDayNumber); }
final int m2; final int y2; if (start.isBefore(end)) { // check the order of the dates d1 = start.getDayOfMonth(); m1 = start.getMonth(); y1 = start.getYYYY(); d2 = end.getDayOfMonth(); m2 = end.getMonth(); y2 = end.getYYYY(); return 360 * (y2 - y1) + 30 * (m2 - m1) + (d2 - d1);
this.date = SerialDate.createInstance( this.date.getDayOfMonth(), c.getSelectedIndex() + 1, this.date.getYYYY() ); refreshButtons(); final JComboBox c = (JComboBox) e.getSource(); final Integer y = (Integer) c.getSelectedItem(); this.date = SerialDate.createInstance( this.date.getDayOfMonth(), this.date.getMonth(), y.intValue() ); refreshYearSelector(); setDate(SerialDate.createInstance(new Date())); final int i = Integer.parseInt(b.getName()); final SerialDate first = getFirstVisibleDate(); final SerialDate selected = SerialDate.addDays(i, first); setDate(selected);
/** * 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 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 true if the specified date is the last day in February (that is, the * 28th in non-leap years, and the 29th in leap years). * * @param d the date to be tested. * * @return a boolean that indicates whether or not the specified date is * the last day of February. */ public static boolean isLastDayOfFebruary(final SerialDate d) { final int dom; if (d.getMonth() == MonthConstants.FEBRUARY) { dom = d.getDayOfMonth(); if (SerialDate.isLeapYear(d.getYYYY())) { return (dom == 29); } else { return (dom == 28); } } else { // not even February return false; } }
/** * 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; }
final int y2; if (start.isOnOrBefore(end)) { // check the order of the dates d1 = start.getDayOfMonth(); m1 = start.getMonth(); y1 = start.getYYYY(); d2 = end.getDayOfMonth(); m2 = end.getMonth(); y2 = end.getYYYY(); if ((d2 == 31) && (d1 == 30)) { // second PSA adjustment d2 = 30;
/** * Returns true if this SerialDate represents the same date as the * specified SerialDate. * * @param other the date being compared to. * * @return <code>true</code> if this SerialDate represents the same date * as the specified SerialDate. */ public boolean isAfter(final SerialDate other) { return (this.serial > other.toSerial()); }
/** * Returns the last millisecond of the day, evaluated using the supplied * calendar (which determines the time zone). * * @param calendar calendar to use (<code>null</code> not permitted). * * @return The end of the day as milliseconds since 01-01-1970. * * @throws NullPointerException if <code>calendar</code> is * <code>null</code>. */ public long getLastMillisecond(Calendar calendar) { int year = this.serialDate.getYYYY(); int month = this.serialDate.getMonth(); int day = this.serialDate.getDayOfMonth(); calendar.clear(); calendar.set(year, month - 1, day, 23, 59, 59); calendar.set(Calendar.MILLISECOND, 999); //return calendar.getTimeInMillis(); // this won't work for JDK 1.3 return calendar.getTime().getTime(); }
/** * Returns the date, given the year. * * @param yyyy the year. * * @return the date generated by this rule for the specified year (null permitted). */ public SerialDate getDate(final int yyyy) { return SerialDate.createInstance(this.dayOfMonth, this.month, yyyy); } }
/** * Returns the month. * * @return The month. */ public int getMonth() { return this.serialDate.getMonth(); }
/** * 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 last millisecond of the month, evaluated using the supplied * calendar (which determines the time zone). * * @param calendar the calendar (<code>null</code> not permitted). * * @return The last millisecond of the month. * * @throws NullPointerException if <code>calendar</code> is * <code>null</code>. */ public long getLastMillisecond(Calendar calendar) { int eom = SerialDate.lastDayOfMonth(this.month, this.year); calendar.set(this.year, this.month - 1, eom, 23, 59, 59); calendar.set(Calendar.MILLISECOND, 999); // in the following line, we'd rather call calendar.getTimeInMillis() // to avoid object creation, but that isn't supported in Java 1.3.1 return calendar.getTime().getTime(); }
/** * Returns a string representing the month (e.g. "January 2002"). * <P> * To do: look at internationalisation. * * @return A string representing the month. */ public String toString() { return SerialDate.monthCodeToString(this.month) + " " + this.year; }
/** * Returns the year. * * @return The year. */ public int getYear() { return this.serialDate.getYYYY(); }
/** * Returns the day of the month. * * @return The day of the month. */ public int getDayOfMonth() { return this.serialDate.getDayOfMonth(); }