Class SerialDate
- All Implemented Interfaces:
Serializable
,Comparable
,MonthConstants
- Direct Known Subclasses:
SpreadsheetDate
Requirement 1 : match at least what Excel does for dates; Requirement 2 : the date represented by the class is immutable;
Why not just use java.util.Date? We will, when it makes sense. At times, java.util.Date can be *too* precise - it represents an instant in time, accurate to 1/1000th of a second (with the date itself depending on the time-zone). Sometimes we just want to represent a particular day (e.g. 21 January 2015) without concerning ourselves about the time of day, or the time-zone, or anything else. That's what we've defined SerialDate for.
You can call getInstance() to get a concrete subclass of SerialDate, without worrying about the exact implementation.
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final DateFormatSymbols
Date format symbols.static final int
A useful constant for referring to the first week in a month.static final int
Useful constant for specifying a day of the week relative to a fixed date.static final int
A useful constant for referring to the fourth week in a month.static final int
Useful constant for Friday.static final int
Useful range constant.static final int
Useful range constant.static final int
Useful range constant.static final int
Useful range constant.static final int
A useful constant for referring to the last week in a month.static final int
The highest year value supported by this date format.static final int
The lowest year value supported by this date format.static final int
Useful constant for Monday.static final int
Useful constant for specifying a day of the week relative to a fixed date.static final int
Useful constant for specifying a day of the week relative to a fixed date.static final int
Useful constant for Saturday.static final int
A useful constant for referring to the second week in a month.static final int
The serial number for 1 January 1900.static final int
The serial number for 31 December 9999.static final int
Useful constant for Sunday.static final int
A useful constant for referring to the third week in a month.static final int
Useful constant for Thrusday.static final int
Useful constant for Tuesday.static final int
Useful constant for Wednesday. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic SerialDate
addDays
(int days, SerialDate base) Creates a new date by adding the specified number of days to the base date.static SerialDate
addMonths
(int months, SerialDate base) Creates a new date by adding the specified number of months to the base date.static SerialDate
addYears
(int years, SerialDate base) Creates a new date by adding the specified number of years to the base date.abstract int
compare
(SerialDate other) Returns the difference (in days) between this date and the specified 'other' date.static SerialDate
createInstance
(int serial) Factory method that returns an instance of some concrete subclass ofSerialDate
.static SerialDate
createInstance
(int day, int month, int yyyy) Factory method that returns an instance of some concrete subclass ofSerialDate
.static SerialDate
createInstance
(Date date) Factory method that returns an instance of a subclass of SerialDate.abstract int
Returns the day of the month.abstract int
Returns the day of the week.Returns the description that is attached to the date.Rolls the date forward to the last day of the month.getFollowingDayOfWeek
(int targetDOW) Returns the earliest date that falls on the specified day-of-the-week and is AFTER this date.static SerialDate
getFollowingDayOfWeek
(int targetWeekday, SerialDate base) Returns the earliest date that falls on the specified day-of-the-week and is AFTER the base date.abstract int
getMonth()
Returns the month (January = 1, February = 2, March = 3).static String[]
Returns an array of month names.static String[]
getMonths
(boolean shortened) Returns an array of month names.getNearestDayOfWeek
(int targetDOW) Returns the nearest date that falls on the specified day-of-the-week.static SerialDate
getNearestDayOfWeek
(int targetDOW, SerialDate base) Returns the date that falls on the specified day-of-the-week and is CLOSEST to the base date.getPreviousDayOfWeek
(int targetDOW) Returns the latest date that falls on the specified day-of-the-week and is BEFORE this date.static SerialDate
getPreviousDayOfWeek
(int targetWeekday, SerialDate base) Returns the latest date that falls on the specified day-of-the-week and is BEFORE the base date.abstract int
getYYYY()
Returns the year (assume a valid range of 1900 to 9999).abstract boolean
isAfter
(SerialDate other) Returns true if this SerialDate represents the same date as the specified SerialDate.abstract boolean
isBefore
(SerialDate other) Returns true if this SerialDate represents an earlier date compared to the specified SerialDate.abstract boolean
isInRange
(SerialDate d1, SerialDate d2) Returnstrue
if thisSerialDate
is within the specified range (INCLUSIVE).abstract boolean
isInRange
(SerialDate d1, SerialDate d2, int include) Returnstrue
if thisSerialDate
is within the specified range (caller specifies whether or not the end-points are included).static boolean
isLeapYear
(int yyyy) Determines whether or not the specified year is a leap year.abstract boolean
isOn
(SerialDate other) Returns true if this SerialDate represents the same date as the specified SerialDate.abstract boolean
isOnOrAfter
(SerialDate other) Returns true if this SerialDate represents the same date as the specified SerialDate.abstract boolean
isOnOrBefore
(SerialDate other) Returns true if this SerialDate represents the same date as the specified SerialDate.static boolean
isValidMonthCode
(int code) Returns true if the supplied integer code represents a valid month.static boolean
isValidWeekdayCode
(int code) Returnstrue
if the supplied integer code represents a valid day-of-the-week, andfalse
otherwise.static boolean
isValidWeekInMonthCode
(int code) Returns true if the supplied integer code represents a valid week-in-the-month, and false otherwise.static int
lastDayOfMonth
(int month, int yyyy) Returns the number of the last day of the month, taking into account leap years.static int
leapYearCount
(int yyyy) Returns the number of leap years from 1900 to the specified year INCLUSIVE.static int
monthCodeToQuarter
(int code) Returns the quarter for the specified month.static String
monthCodeToString
(int month) Returns a string representing the supplied month.static String
monthCodeToString
(int month, boolean shortened) Returns a string representing the supplied month.static String
relativeToString
(int relative) Returns a string representing the supplied 'relative'.void
setDescription
(String description) Sets the description for the date.static int
Converts a string to a month code.static int
Converts the supplied string to a day of the week.abstract Date
toDate()
Returns a java.util.Date.abstract 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).toString()
Converts the date to a string.static String
weekdayCodeToString
(int weekday) Returns a string representing the supplied day-of-the-week.static String
weekInMonthToString
(int count) Returns a string corresponding to the week-in-the-month code.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Comparable
compareTo
-
Field Details
-
DATE_FORMAT_SYMBOLS
Date format symbols. -
SERIAL_LOWER_BOUND
The serial number for 1 January 1900.- See Also:
-
SERIAL_UPPER_BOUND
The serial number for 31 December 9999.- See Also:
-
MINIMUM_YEAR_SUPPORTED
The lowest year value supported by this date format.- See Also:
-
MAXIMUM_YEAR_SUPPORTED
The highest year value supported by this date format.- See Also:
-
MONDAY
Useful constant for Monday. Equivalent to java.util.Calendar.MONDAY.- See Also:
-
TUESDAY
Useful constant for Tuesday. Equivalent to java.util.Calendar.TUESDAY.- See Also:
-
WEDNESDAY
Useful constant for Wednesday. Equivalent to java.util.Calendar.WEDNESDAY.- See Also:
-
THURSDAY
Useful constant for Thrusday. Equivalent to java.util.Calendar.THURSDAY.- See Also:
-
FRIDAY
Useful constant for Friday. Equivalent to java.util.Calendar.FRIDAY.- See Also:
-
SATURDAY
Useful constant for Saturday. Equivalent to java.util.Calendar.SATURDAY.- See Also:
-
SUNDAY
Useful constant for Sunday. Equivalent to java.util.Calendar.SUNDAY.- See Also:
-
FIRST_WEEK_IN_MONTH
A useful constant for referring to the first week in a month.- See Also:
-
SECOND_WEEK_IN_MONTH
A useful constant for referring to the second week in a month.- See Also:
-
THIRD_WEEK_IN_MONTH
A useful constant for referring to the third week in a month.- See Also:
-
FOURTH_WEEK_IN_MONTH
A useful constant for referring to the fourth week in a month.- See Also:
-
LAST_WEEK_IN_MONTH
A useful constant for referring to the last week in a month.- See Also:
-
INCLUDE_NONE
Useful range constant.- See Also:
-
INCLUDE_FIRST
Useful range constant.- See Also:
-
INCLUDE_SECOND
Useful range constant.- See Also:
-
INCLUDE_BOTH
Useful range constant.- See Also:
-
PRECEDING
Useful constant for specifying a day of the week relative to a fixed date.- See Also:
-
NEAREST
Useful constant for specifying a day of the week relative to a fixed date.- See Also:
-
FOLLOWING
Useful constant for specifying a day of the week relative to a fixed date.- See Also:
-
-
Constructor Details
-
SerialDate
protected SerialDate()Default constructor.
-
-
Method Details
-
isValidWeekdayCode
Returnstrue
if the supplied integer code represents a valid day-of-the-week, andfalse
otherwise.- Parameters:
code
- the code being checked for validity.- Returns:
true
if the supplied integer code represents a valid day-of-the-week, andfalse
otherwise.
-
stringToWeekdayCode
Converts the supplied string to a day of the week.- Parameters:
s
- a string representing the day of the week.- Returns:
-1
if the string is not convertable, the day of the week otherwise.
-
weekdayCodeToString
Returns a string representing the supplied day-of-the-week.Need to find a better approach.
- Parameters:
weekday
- the day of the week.- Returns:
- a string representing the supplied day-of-the-week.
-
getMonths
Returns an array of month names.- Returns:
- an array of month names.
-
getMonths
Returns an array of month names.- Parameters:
shortened
- a flag indicating that shortened month names should be returned.- Returns:
- an array of month names.
-
isValidMonthCode
Returns true if the supplied integer code represents a valid month.- Parameters:
code
- the code being checked for validity.- Returns:
true
if the supplied integer code represents a valid month.
-
monthCodeToQuarter
Returns the quarter for the specified month.- Parameters:
code
- the month code (1-12).- Returns:
- the quarter that the month belongs to.
-
monthCodeToString
Returns a string representing the supplied month.The string returned is the long form of the month name taken from the default locale.
- Parameters:
month
- the month.- Returns:
- a string representing the supplied month.
-
monthCodeToString
Returns a string representing the supplied month.The string returned is the long or short form of the month name taken from the default locale.
- Parameters:
month
- the month.shortened
- iftrue
return the abbreviation of the month.- Returns:
- a string representing the supplied month.
-
stringToMonthCode
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.
- Parameters:
s
- the string to parse.- Returns:
-1
if the string is not parseable, the month of the year otherwise.
-
isValidWeekInMonthCode
Returns true if the supplied integer code represents a valid week-in-the-month, and false otherwise.- Parameters:
code
- the code being checked for validity.- Returns:
true
if the supplied integer code represents a valid week-in-the-month.
-
isLeapYear
Determines whether or not the specified year is a leap year.- Parameters:
yyyy
- the year (in the range 1900 to 9999).- Returns:
true
if the specified year is a leap year.
-
leapYearCount
Returns the number of leap years from 1900 to the specified year INCLUSIVE.Note that 1900 is not a leap year.
- Parameters:
yyyy
- the year (in the range 1900 to 9999).- Returns:
- the number of leap years from 1900 to the specified year.
-
lastDayOfMonth
Returns the number of the last day of the month, taking into account leap years.- Parameters:
month
- the month.yyyy
- the year (in the range 1900 to 9999).- Returns:
- the number of the last day of the month.
-
addDays
Creates a new date by adding the specified number of days to the base date.- Parameters:
days
- the number of days to add (can be negative).base
- the base date.- Returns:
- a new date.
-
addMonths
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.
- Parameters:
months
- the number of months to add (can be negative).base
- the base date.- Returns:
- a new date.
-
addYears
Creates a new date by adding the specified number of years to the base date.- Parameters:
years
- the number of years to add (can be negative).base
- the base date.- Returns:
- A new date.
-
getPreviousDayOfWeek
Returns the latest date that falls on the specified day-of-the-week and is BEFORE the base date.- Parameters:
targetWeekday
- a code for the target day-of-the-week.base
- the base date.- Returns:
- the latest date that falls on the specified day-of-the-week and is BEFORE the base date.
-
getFollowingDayOfWeek
Returns the earliest date that falls on the specified day-of-the-week and is AFTER the base date.- Parameters:
targetWeekday
- a code for the target day-of-the-week.base
- the base date.- Returns:
- the earliest date that falls on the specified day-of-the-week and is AFTER the base date.
-
getNearestDayOfWeek
Returns the date that falls on the specified day-of-the-week and is CLOSEST to the base date.- Parameters:
targetDOW
- a code for the target day-of-the-week.base
- the base date.- Returns:
- the date that falls on the specified day-of-the-week and is CLOSEST to the base date.
-
getEndOfCurrentMonth
Rolls the date forward to the last day of the month.- Parameters:
base
- the base date.- Returns:
- a new serial date.
-
weekInMonthToString
Returns a string corresponding to the week-in-the-month code.Need to find a better approach.
- Parameters:
count
- an integer code representing the week-in-the-month.- Returns:
- a string corresponding to the week-in-the-month code.
-
relativeToString
Returns a string representing the supplied 'relative'.Need to find a better approach.
- Parameters:
relative
- a constant representing the 'relative'.- Returns:
- a string representing the supplied 'relative'.
-
createInstance
Factory method that returns an instance of some concrete subclass ofSerialDate
.- Parameters:
day
- the day (1-31).month
- the month (1-12).yyyy
- the year (in the range 1900 to 9999).- Returns:
- An instance of
SerialDate
.
-
createInstance
Factory method that returns an instance of some concrete subclass ofSerialDate
.- Parameters:
serial
- the serial number for the day (1 January 1900 = 2).- Returns:
- a instance of SerialDate.
-
createInstance
Factory method that returns an instance of a subclass of SerialDate.- Parameters:
date
- A Java date object.- Returns:
- a instance of SerialDate.
-
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).- Returns:
- the serial number for the 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'.- Returns:
- this as
java.util.Date
.
-
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.- Returns:
- The description (possibly
null
).
-
setDescription
Sets the description for the date.- Parameters:
description
- the description for this date (null
permitted).
-
toString
Converts the date to a string. -
getYYYY
Returns the year (assume a valid range of 1900 to 9999).- Returns:
- the year.
-
getMonth
Returns the month (January = 1, February = 2, March = 3).- Returns:
- the month of the year.
-
getDayOfMonth
Returns the day of the month.- Returns:
- the day of the month.
-
getDayOfWeek
Returns the day of the week.- Returns:
- the day of the week.
-
compare
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.
- Parameters:
other
- the date being compared to.- Returns:
- the difference between this and the other date.
-
isOn
Returns true if this SerialDate represents the same date as the specified SerialDate.- Parameters:
other
- the date being compared to.- Returns:
true
if this SerialDate represents the same date as the specified SerialDate.
-
isBefore
Returns true if this SerialDate represents an earlier date compared to the specified SerialDate.- Parameters:
other
- The date being compared to.- Returns:
true
if this SerialDate represents an earlier date compared to the specified SerialDate.
-
isOnOrBefore
Returns true if this SerialDate represents the same date as the specified SerialDate.- Parameters:
other
- the date being compared to.- Returns:
true
if this SerialDate represents the same date as the specified SerialDate.
-
isAfter
Returns true if this SerialDate represents the same date as the specified SerialDate.- Parameters:
other
- the date being compared to.- Returns:
true
if this SerialDate represents the same date as the specified SerialDate.
-
isOnOrAfter
Returns true if this SerialDate represents the same date as the specified SerialDate.- Parameters:
other
- the date being compared to.- Returns:
true
if this SerialDate represents the same date as the specified SerialDate.
-
isInRange
Returnstrue
if thisSerialDate
is within the specified range (INCLUSIVE). The date order of d1 and d2 is not important.- Parameters:
d1
- a boundary date for the range.d2
- the other boundary date for the range.- Returns:
- A boolean.
-
isInRange
Returnstrue
if thisSerialDate
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.- Parameters:
d1
- a boundary date for the range.d2
- the other boundary date for the range.include
- a code that controls whether or not the start and end dates are included in the range.- Returns:
- A boolean.
-
getPreviousDayOfWeek
Returns the latest date that falls on the specified day-of-the-week and is BEFORE this date.- Parameters:
targetDOW
- a code for the target day-of-the-week.- Returns:
- the latest date that falls on the specified day-of-the-week and is BEFORE this date.
-
getFollowingDayOfWeek
Returns the earliest date that falls on the specified day-of-the-week and is AFTER this date.- Parameters:
targetDOW
- a code for the target day-of-the-week.- Returns:
- the earliest date that falls on the specified day-of-the-week and is AFTER this date.
-
getNearestDayOfWeek
Returns the nearest date that falls on the specified day-of-the-week.- Parameters:
targetDOW
- a code for the target day-of-the-week.- Returns:
- the nearest date that falls on the specified day-of-the-week.
-