com.jrefinery.date
Class SpreadsheetDate

java.lang.Object
  extended by com.jrefinery.date.SerialDate
      extended by com.jrefinery.date.SpreadsheetDate
All Implemented Interfaces:
MonthConstants

public class SpreadsheetDate
extends SerialDate

Represents a date using an integer, in a similar fashion to the implementation in Microsoft Excel. The range of dates supported is 1-Jan-1900 to 31-Dec-9999.

Be aware that there is a deliberate bug in Excel that recognises the year 1900 as a leap year when in fact it is not a leap year. You can find more information on the Microsoft website in article Q181370:

http://support.microsoft.com/support/kb/articles/Q181/3/70.asp

Excel uses the convention that 1-Jan-1900 = 1. This class uses the convention 1-Jan-1900 = 2. The result is that the day number in this class will be different to the Excel figure for January and February 1900...but then Excel adds in an extra day (29-Feb-1900 which does not actually exist!) and from that point forward the day numbers will match.


Field Summary
protected  int day
          The day of the month (1 to 28, 29, 30 or 31 depending on the month).
protected  java.lang.String description
          An optional description for the date.
protected  int month
          The month of the year (1 to 12).
protected  int serial
          The day number (1-Jan-1900 = 2, 2-Jan-1900 = 3, ..., 31-Dec-9999 = 2958465).
protected  int year
          The year (1900 to 9999).
 
Fields inherited from class com.jrefinery.date.SerialDate
AGGREGATE_DAYS_TO_END_OF_MONTH, AGGREGATE_DAYS_TO_END_OF_PRECEDING_MONTH, DATE_FORMAT_SYMBOLS, FIRST_WEEK_IN_MONTH, FOLLOWING, FOURTH_WEEK_IN_MONTH, FRIDAY, INCLUDE_BOTH, INCLUDE_FIRST, INCLUDE_NONE, INCLUDE_SECOND, LAST_DAY_OF_MONTH, LAST_WEEK_IN_MONTH, LEAP_YEAR_AGGREGATE_DAYS_TO_END_OF_MONTH, LEAP_YEAR_AGGREGATE_DAYS_TO_END_OF_PRECEDING_MONTH, MAXIMUM_YEAR_SUPPORTED, MINIMUM_YEAR_SUPPORTED, MONDAY, NEAREST, PRECEDING, SATURDAY, SECOND_WEEK_IN_MONTH, SERIAL_LOWER_BOUND, SERIAL_UPPER_BOUND, SUNDAY, THIRD_WEEK_IN_MONTH, THURSDAY, TUESDAY, WEDNESDAY
 
Fields inherited from interface com.jrefinery.date.MonthConstants
APRIL, AUGUST, DECEMBER, FEBRUARY, JANUARY, JULY, JUNE, MARCH, MAY, NOVEMBER, OCTOBER, SEPTEMBER
 
Constructor Summary
SpreadsheetDate(int serial)
          Standard constructor - creates a new date object representing the specified day number (which should be in the range 2 to 2958465.
SpreadsheetDate(int day, int month, int year)
          Constructs a new spreadsheet date.
 
Method Summary
 int compare(SerialDate other)
          Returns the difference (in days) between this date and the specified 'other' date.
 boolean equals(java.lang.Object object)
          Tests the equality of this SpreadsheetDate with an arbitrary Object.
 int getDayOfMonth()
          Returns the day of the month.
 int getDayOfWeek()
          Returns a code representing the day of the week.
 java.lang.String getDescription()
          Returns the description that is attached to the date.
 int getMonth()
          Returns the month (January = 1, February = 2, March = 3).
 int getYYYY()
          Returns the year (assume a valid range of 1900 to 9999).
 boolean isAfter(SerialDate other)
          Returns true if this SerialDate represents the same date as the specified SerialDate.
 boolean isBefore(SerialDate other)
          Returns true if this SerialDate represents an earlier date compared to the specified SerialDate.
 boolean isInRange(SerialDate d1, SerialDate d2)
          Returns true if this SerialDate is within the specified range (INCLUSIVE).
 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).
 boolean isOn(SerialDate other)
          Returns true if this SerialDate represents the same date as the specified SerialDate.
 boolean isOnOrAfter(SerialDate other)
          Returns true if this SerialDate represents the same date as the specified SerialDate.
 boolean isOnOrBefore(SerialDate other)
          Returns true if this SerialDate represents the same date as the specified SerialDate.
static void main(java.lang.String[] args)
          Testing code - please ignore...
 void setDescription(java.lang.String description)
          Sets the description for the date.
 java.util.Date toDate()
          Returns a java.util.Date equivalent to this date.
 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).
 
Methods inherited from class com.jrefinery.date.SerialDate
addDays, addMonths, addYears, createInstance, createInstance, createInstance, getEndOfCurrentMonth, getFollowingDayOfWeek, getFollowingDayOfWeek, getMonths, getMonths, getNearestDayOfWeek, getNearestDayOfWeek, getPreviousDayOfWeek, getPreviousDayOfWeek, isLeapYear, isValidMonthCode, isValidWeekdayCode, isValidWeekInMonthCode, lastDayOfMonth, leapYearCount, monthCodeToQuarter, monthCodeToString, monthCodeToString, relativeToString, stringToMonthCode, stringToWeekdayCode, toString, weekdayCodeToString, weekInMonthToString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

serial

protected int serial
The day number (1-Jan-1900 = 2, 2-Jan-1900 = 3, ..., 31-Dec-9999 = 2958465).


day

protected int day
The day of the month (1 to 28, 29, 30 or 31 depending on the month).


month

protected int month
The month of the year (1 to 12).


year

protected int year
The year (1900 to 9999).


description

protected java.lang.String description
An optional description for the date.

Constructor Detail

SpreadsheetDate

public SpreadsheetDate(int day,
                       int month,
                       int year)
Constructs a new spreadsheet date.

Parameters:
day - The day.
month - The month (1-12).
year - The year (in the range 1900 to 9999).

SpreadsheetDate

public SpreadsheetDate(int serial)
Standard constructor - creates a new date object representing the specified day number (which should be in the range 2 to 2958465.

Parameters:
serial - The serial number for the day (range: 2 to 2958465).
Method Detail

getDescription

public java.lang.String getDescription()
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.

Overrides:
getDescription in class SerialDate
Returns:
The description that is attached to the date.

setDescription

public void setDescription(java.lang.String description)
Sets the description for the date.

Overrides:
setDescription in class SerialDate
Parameters:
description - The description for this date.

toSerial

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).

Specified by:
toSerial in class SerialDate

toDate

public java.util.Date toDate()
Returns a java.util.Date equivalent to this date.

Specified by:
toDate in class SerialDate

getYYYY

public int getYYYY()
Returns the year (assume a valid range of 1900 to 9999).

Specified by:
getYYYY in class SerialDate

getMonth

public int getMonth()
Returns the month (January = 1, February = 2, March = 3).

Specified by:
getMonth in class SerialDate

getDayOfMonth

public int getDayOfMonth()
Returns the day of the month.

Specified by:
getDayOfMonth in class SerialDate

getDayOfWeek

public int getDayOfWeek()
Returns a code representing the day of the week.

The codes are defined in the SerialDate class as: SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY.

Specified by:
getDayOfWeek in class SerialDate
Returns:
A code representing the day of the week.

equals

public boolean equals(java.lang.Object object)
Tests the equality of this SpreadsheetDate with an arbitrary Object.

This method will return true ONLY if the object is an instance of the SerialDate base class, and it represents the same day as this SpreadsheetDate.

Overrides:
equals in class java.lang.Object

compare

public int compare(SerialDate other)
Returns the difference (in days) between this date and the specified 'other' date.

Specified by:
compare in class SerialDate
Parameters:
other - The date being compared to.

isOn

public boolean isOn(SerialDate other)
Returns true if this SerialDate represents the same date as the specified SerialDate.

Specified by:
isOn in class SerialDate
Parameters:
other - The date being compared to.

isBefore

public boolean isBefore(SerialDate other)
Returns true if this SerialDate represents an earlier date compared to the specified SerialDate.

Specified by:
isBefore in class SerialDate
Parameters:
other - The date being compared to.

isOnOrBefore

public boolean isOnOrBefore(SerialDate other)
Returns true if this SerialDate represents the same date as the specified SerialDate.

Specified by:
isOnOrBefore in class SerialDate
Parameters:
other - The date being compared to.

isAfter

public boolean isAfter(SerialDate other)
Returns true if this SerialDate represents the same date as the specified SerialDate.

Specified by:
isAfter in class SerialDate
Parameters:
other - The date being compared to.

isOnOrAfter

public boolean isOnOrAfter(SerialDate other)
Returns true if this SerialDate represents the same date as the specified SerialDate.

Specified by:
isOnOrAfter in class SerialDate
Parameters:
other - The date being compared to.

isInRange

public boolean isInRange(SerialDate d1,
                         SerialDate d2)
Returns true if this SerialDate is within the specified range (INCLUSIVE). The order of d1 d2 is not important.

Specified by:
isInRange in class SerialDate
Parameters:
d1 - One boundary date for the range.
d2 - A second boundary date for the range.

isInRange

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 order of d1 and d2 is not important.

Specified by:
isInRange in class SerialDate
Parameters:
d1 - One boundary date for the range.
d2 - A second boundary date for the range.

main

public static void main(java.lang.String[] args)
Testing code - please ignore...



Copyright © 2003 MBARI.

The Monterey Bay Aquarium Research Institute (MBARI) provides this documentation and code "as is", with no warranty, express or implied, of its quality or consistency. It is provided without support and without obligation on the part of MBARI to assist in its use, correction, modification, or enhancement.