📄 serialdate.java
字号:
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);
}
/**
* Rolls the date forward to the last day of the month.
*
* @param base the base date.
*
* @return a new serial date.
*/
public SerialDate getEndOfCurrentMonth(SerialDate base) {
int last = SerialDate.lastDayOfMonth(base.getMonth(), base.getYYYY());
return SerialDate.createInstance(last, base.getMonth(), base.getYYYY());
}
/**
* Returns a string corresponding to the week-in-the-month code.
* <P>
* Need to find a better approach.
*
* @param count an integer code representing the week-in-the-month.
*
* @return a string corresponding to the week-in-the-month code.
*/
public static String weekInMonthToString(int count) {
switch (count) {
case SerialDate.FIRST_WEEK_IN_MONTH : return "First";
case SerialDate.SECOND_WEEK_IN_MONTH : return "Second";
case SerialDate.THIRD_WEEK_IN_MONTH : return "Third";
case SerialDate.FOURTH_WEEK_IN_MONTH : return "Fourth";
case SerialDate.LAST_WEEK_IN_MONTH : return "Last";
default :
return "SerialDate.weekInMonthToString(...): invalid code.";
}
}
/**
* Returns a string representing the supplied 'relative'.
* <P>
* Need to find a better approach.
*
* @param relative a constant representing the 'relative'.
*
* @return a string representing the supplied 'relative'.
*/
public static String relativeToString(int relative) {
switch (relative) {
case SerialDate.PRECEDING : return "Preceding";
case SerialDate.NEAREST : return "Nearest";
case SerialDate.FOLLOWING : return "Following";
default : return "ERROR : Relative To String";
}
}
/**
* Factory method that returns an instance of some concrete subclass of {@link SerialDate}.
*
* @param day the day (1-31).
* @param month the month (1-12).
* @param yyyy the year (in the range 1900 to 9999).
*
* @return a instance of SerialDate.
*/
public static SerialDate createInstance(int day, int month, int yyyy) {
return new SpreadsheetDate(day, month, yyyy);
}
/**
* Factory method that returns an instance of some concrete subclass of {@link SerialDate}.
*
* @param serial the serial number for the day (1 January 1900 = 2).
*
* @return a instance of SerialDate.
*/
public static SerialDate createInstance(int serial) {
return new SpreadsheetDate(serial);
}
/**
* Factory method that returns an instance of a subclass of SerialDate.
*
* @param date A Java date object.
*
* @return a instance of SerialDate.
*/
public static SerialDate createInstance(java.util.Date date) {
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(date);
return new SpreadsheetDate(calendar.get(Calendar.DATE),
calendar.get(Calendar.MONTH) + 1,
calendar.get(Calendar.YEAR));
}
/**
* Returns the serial number for the date, where 1 January 1900 = 2 (this
* corresponds, almost, to the numbering system used in Microsoft Excel for
* Windows and Lotus 1-2-3).
*
* @return the serial number for the date.
*/
public abstract int toSerial();
/**
* Returns a java.util.Date. Since java.util.Date has more precision than
* SerialDate, we need to define a convention for the 'time of day'.
*
* @return this as <code>java.util.Date</code>.
*/
public abstract java.util.Date toDate();
/**
* Returns a description of the date.
*
* @return a description of the date.
*/
public String getDescription() {
return this.description;
}
/**
* Sets the description for the date.
*
* @param description the new description for the date.
*/
public void setDescription(String description) {
this.description = description;
}
/**
* Converts the date to a string.
*
* @return a string representation of the date.
*/
public String toString() {
return getDayOfMonth() + "-" + SerialDate.monthCodeToString(getMonth())
+ "-" + getYYYY();
}
/**
* Returns the year (assume a valid range of 1900 to 9999).
*
* @return the year.
*/
public abstract int getYYYY();
/**
* Returns the month (January = 1, February = 2, March = 3).
*
* @return the month of the year.
*/
public abstract int getMonth();
/**
* Returns the day of the month.
*
* @return the day of the month.
*/
public abstract int getDayOfMonth();
/**
* Returns the day of the week.
*
* @return the day of the week.
*/
public abstract int getDayOfWeek();
/**
* Returns the difference (in days) between this date and the specified 'other' date.
* <P>
* The result is positive if this date is after the 'other' date and
* negative if it is before the 'other' date.
*
* @param other the date being compared to.
*
* @return the difference between this and the other date.
*/
public abstract int compare(SerialDate other);
/**
* 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 abstract boolean isOn(SerialDate other);
/**
* Returns true if this SerialDate represents an earlier date compared to
* the specified SerialDate.
*
* @param other The date being compared to.
*
* @return <code>true</code> if this SerialDate represents an earlier date compared to the
* specified SerialDate.
*/
public abstract boolean isBefore(SerialDate other);
/**
* 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 abstract boolean isOnOrBefore(SerialDate other);
/**
* 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 abstract boolean isAfter(SerialDate other);
/**
* 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 abstract boolean isOnOrAfter(SerialDate other);
/**
* Returns true if this SerialDate is within the specified range (INCLUSIVE). The order of
* d1 d2 is not important.
*
* @param d1 one boundary date for the range.
* @param d2 a second boundary date for the range.
*
* @return <code>true</code> if this SerialDate is within the specified range.
*/
public abstract boolean isInRange(SerialDate d1, SerialDate d2);
/**
* Returns true if this SerialDate is within the specified range (caller
* specifies whether or not the end-points are included). The order of d1
* and d2 is not important.
*
* @param d1 one boundary date for the range.
* @param d2 a second boundary date for the range.
* @param include if <code>true</code> include d2 in the search.
*
* @return <code>true</code> if this SerialDate is within the specified range.
*/
public abstract boolean isInRange(SerialDate d1, SerialDate d2, int include);
/**
* Returns the latest date that falls on the specified day-of-the-week and
* is BEFORE this date.
*
* @param targetDOW a code for the target day-of-the-week.
*
* @return the latest date that falls on the specified day-of-the-week and
* is BEFORE this date.
*/
public SerialDate getPreviousDayOfWeek(int targetDOW) {
return getPreviousDayOfWeek(targetDOW, this);
}
/**
* Returns the earliest date that falls on the specified day-of-the-week
* and is AFTER this date.
*
* @param targetDOW a code for the target day-of-the-week.
*
* @return the earliest date that falls on the specified day-of-the-week
* and is AFTER this date.
*/
public SerialDate getFollowingDayOfWeek(int targetDOW) {
return getFollowingDayOfWeek(targetDOW, this);
}
/**
* Returns the nearest date that falls on the specified day-of-the-week.
*
* @param targetDOW a code for the target day-of-the-week.
*
* @return the nearest date that falls on the specified day-of-the-week.
*/
public SerialDate getNearestDayOfWeek(int targetDOW) {
return getNearestDayOfWeek(targetDOW, this);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -