| Method from org.jfree.date.SerialDate Detail: |
public static SerialDate addDays(int days,
SerialDate base) {
final int serialDayNumber = base.toSerial() + days;
return SerialDate.createInstance(serialDayNumber);
}
Creates a new date by adding the specified number of days to the base
date. |
public static SerialDate addMonths(int months,
SerialDate base) {
final int yy = (12 * base.getYYYY() + base.getMonth() + months - 1)
/ 12;
final int mm = (12 * base.getYYYY() + base.getMonth() + months - 1)
% 12 + 1;
final int dd = Math.min(
base.getDayOfMonth(), SerialDate.lastDayOfMonth(mm, yy)
);
return SerialDate.createInstance(dd, mm, yy);
}
Creates a new date by adding the specified number of months to the base
date.
If the base date is close to the end of the month, the day on the result
may be adjusted slightly: 31 May + 1 month = 30 June. |
public static SerialDate addYears(int years,
SerialDate base) {
final int baseY = base.getYYYY();
final int baseM = base.getMonth();
final int baseD = base.getDayOfMonth();
final int targetY = baseY + years;
final int targetD = Math.min(
baseD, SerialDate.lastDayOfMonth(baseM, targetY)
);
return SerialDate.createInstance(targetD, baseM, targetY);
}
Creates a new date by adding the specified number of years to the base
date. |
abstract public int compare(SerialDate other)
Returns the difference (in days) between this date and the specified
'other' date.
The result is positive if this date is after the 'other' date and
negative if it is before the 'other' date. |
public static SerialDate createInstance(int serial) {
return new SpreadsheetDate(serial);
}
Factory method that returns an instance of some concrete subclass of
SerialDate . |
public static SerialDate createInstance(Date date) {
final GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(date);
return new SpreadsheetDate(calendar.get(Calendar.DATE),
calendar.get(Calendar.MONTH) + 1,
calendar.get(Calendar.YEAR));
}
Factory method that returns an instance of a subclass 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
SerialDate . |
abstract public int getDayOfMonth()
Returns the day of the month. |
abstract public int getDayOfWeek()
Returns the day of the week. |
public String getDescription() {
return this.description;
}
Returns the description that is attached to the date. It is not
required that a date have a description, but for some applications it
is useful. |
public SerialDate getEndOfCurrentMonth(SerialDate base) {
final int last = SerialDate.lastDayOfMonth(
base.getMonth(), base.getYYYY()
);
return SerialDate.createInstance(last, base.getMonth(), base.getYYYY());
}
Rolls the date forward to the last day of the month. |
public SerialDate getFollowingDayOfWeek(int targetDOW) {
return getFollowingDayOfWeek(targetDOW, this);
}
Returns the earliest date that falls on the specified day-of-the-week
and is AFTER this date. |
public static SerialDate getFollowingDayOfWeek(int targetWeekday,
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 earliest date that falls on the specified day-of-the-week
and is AFTER the base date. |
abstract public int getMonth()
Returns the month (January = 1, February = 2, March = 3). |
public static String[] getMonths() {
return getMonths(false);
}
Returns an array of month names. |
public static String[] getMonths(boolean shortened) {
if (shortened) {
return DATE_FORMAT_SYMBOLS.getShortMonths();
}
else {
return DATE_FORMAT_SYMBOLS.getMonths();
}
}
Returns an array of month names. |
public SerialDate getNearestDayOfWeek(int targetDOW) {
return getNearestDayOfWeek(targetDOW, this);
}
Returns the nearest date that falls on the specified day-of-the-week. |
public static SerialDate getNearestDayOfWeek(int targetDOW,
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 date that falls on the specified day-of-the-week and is
CLOSEST to the base date. |
public SerialDate getPreviousDayOfWeek(int targetDOW) {
return getPreviousDayOfWeek(targetDOW, this);
}
Returns the latest date that falls on the specified day-of-the-week and
is BEFORE this date. |
public static SerialDate getPreviousDayOfWeek(int targetWeekday,
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 latest date that falls on the specified day-of-the-week and
is BEFORE the base date. |
abstract public int getYYYY()
Returns the year (assume a valid range of 1900 to 9999). |
abstract public boolean isAfter(SerialDate other)
Returns true if this SerialDate represents the same date as the
specified SerialDate. |
abstract public boolean isBefore(SerialDate other)
Returns true if this SerialDate represents an earlier date compared to
the specified SerialDate. |
abstract public boolean isInRange(SerialDate d1,
SerialDate d2)
Returns true if this SerialDate is within the
specified range (INCLUSIVE). The date order of d1 and d2 is not
important. |
abstract public boolean isInRange(SerialDate d1,
SerialDate d2,
int include)
Returns true if this SerialDate is within the
specified range (caller specifies whether or not the end-points are
included). The date order of d1 and d2 is not important. |
public static boolean isLeapYear(int yyyy) {
if ((yyyy % 4) != 0) {
return false;
}
else if ((yyyy % 400) == 0) {
return true;
}
else if ((yyyy % 100) == 0) {
return false;
}
else {
return true;
}
}
Determines whether or not the specified year is a leap year. |
abstract public boolean isOn(SerialDate other)
Returns true if this SerialDate represents the same date as the
specified SerialDate. |
abstract public boolean isOnOrAfter(SerialDate other)
Returns true if this SerialDate represents the same date as the
specified SerialDate. |
abstract public boolean isOnOrBefore(SerialDate other)
Returns true if this SerialDate represents the same date as the
specified SerialDate. |
public static boolean isValidMonthCode(int code) {
switch(code) {
case JANUARY:
case FEBRUARY:
case MARCH:
case APRIL:
case MAY:
case JUNE:
case JULY:
case AUGUST:
case SEPTEMBER:
case OCTOBER:
case NOVEMBER:
case DECEMBER:
return true;
default:
return false;
}
}
Returns true if the supplied integer code represents a valid month. |
public static boolean isValidWeekInMonthCode(int code) {
switch(code) {
case FIRST_WEEK_IN_MONTH:
case SECOND_WEEK_IN_MONTH:
case THIRD_WEEK_IN_MONTH:
case FOURTH_WEEK_IN_MONTH:
case LAST_WEEK_IN_MONTH: return true;
default: return false;
}
}
Returns true if the supplied integer code represents a valid
week-in-the-month, and false otherwise. |
public static boolean isValidWeekdayCode(int code) {
switch(code) {
case SUNDAY:
case MONDAY:
case TUESDAY:
case WEDNESDAY:
case THURSDAY:
case FRIDAY:
case SATURDAY:
return true;
default:
return false;
}
}
Returns true if the supplied integer code represents a
valid day-of-the-week, and false otherwise. |
public static int lastDayOfMonth(int month,
int yyyy) {
final int result = LAST_DAY_OF_MONTH[month];
if (month != FEBRUARY) {
return result;
}
else if (isLeapYear(yyyy)) {
return result + 1;
}
else {
return result;
}
}
Returns the number of the last day of the month, taking into account
leap years. |
public static int leapYearCount(int yyyy) {
final int leap4 = (yyyy - 1896) / 4;
final int leap100 = (yyyy - 1800) / 100;
final int leap400 = (yyyy - 1600) / 400;
return leap4 - leap100 + leap400;
}
|
public static int monthCodeToQuarter(int code) {
switch(code) {
case JANUARY:
case FEBRUARY:
case MARCH: return 1;
case APRIL:
case MAY:
case JUNE: return 2;
case JULY:
case AUGUST:
case SEPTEMBER: return 3;
case OCTOBER:
case NOVEMBER:
case DECEMBER: return 4;
default: throw new IllegalArgumentException(
"SerialDate.monthCodeToQuarter: invalid month code.");
}
}
Returns the quarter for the specified month. |
public static String monthCodeToString(int month) {
return monthCodeToString(month, false);
}
|
public static String monthCodeToString(int month,
boolean shortened) {
// check arguments...
if (!isValidMonthCode(month)) {
throw new IllegalArgumentException(
"SerialDate.monthCodeToString: month outside valid range.");
}
final String[] months;
if (shortened) {
months = DATE_FORMAT_SYMBOLS.getShortMonths();
}
else {
months = DATE_FORMAT_SYMBOLS.getMonths();
}
return months[month - 1];
}
|
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";
}
}
|
public void setDescription(String description) {
this.description = description;
}
Sets the description for the date. |
public static int stringToMonthCode(String s) {
final String[] shortMonthNames = DATE_FORMAT_SYMBOLS.getShortMonths();
final String[] monthNames = DATE_FORMAT_SYMBOLS.getMonths();
int result = -1;
s = s.trim();
// first try parsing the string as an integer (1-12)...
try {
result = Integer.parseInt(s);
}
catch (NumberFormatException e) {
// suppress
}
// now search through the month names...
if ((result < 1) || (result > 12)) {
for (int i = 0; i < monthNames.length; i++) {
if (s.equals(shortMonthNames[i])) {
result = i + 1;
break;
}
if (s.equals(monthNames[i])) {
result = i + 1;
break;
}
}
}
return result;
}
Converts a string to a month code.
This method will return one of the constants JANUARY, FEBRUARY, ...,
DECEMBER that corresponds to the string. If the string is not
recognised, this method returns -1. |
public static int stringToWeekdayCode(String s) {
final String[] shortWeekdayNames
= DATE_FORMAT_SYMBOLS.getShortWeekdays();
final String[] weekDayNames = DATE_FORMAT_SYMBOLS.getWeekdays();
int result = -1;
s = s.trim();
for (int i = 0; i < weekDayNames.length; i++) {
if (s.equals(shortWeekdayNames[i])) {
result = i;
break;
}
if (s.equals(weekDayNames[i])) {
result = i;
break;
}
}
return result;
}
Converts the supplied string to a day of the week. |
abstract public Date toDate()
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'. |
abstract public int toSerial()
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). |
public String toString() {
return getDayOfMonth() + "-" + SerialDate.monthCodeToString(getMonth())
+ "-" + getYYYY();
}
Converts the date to a string. |
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.";
}
}
|
public static String weekdayCodeToString(int weekday) {
final String[] weekdays = DATE_FORMAT_SYMBOLS.getWeekdays();
return weekdays[weekday];
}
|