|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.sourceforge.calendardate.CalendarDate
This class represents a date in the Gregorian calendar (for example, December
20, 1998). It is designed to be a simpler, immutable version of
java.util.GregorianCalendar
.
Don't confuse this class with java.util.Date
! They
represent two separate things:
CalendarDate
represents a 'date in the calendar' (e.g.
"John Smith's birthday") while
java.util.Date
represent an 'instant in time' (e.g. "When
order 23711 was received"). A better name for this class would have been
'DateTime'.
java.util.TimeZone
when converting between instances of
CalendarDate
and Date
.)
This class is thread-safe and immutable.
Date
,
GregorianCalendar
,
Serialized FormField Summary | |
static int |
APRIL
The value returned by getMonth() representing April |
static int |
AUGUST
The value returned by getMonth() representing August |
static int |
DECEMBER
The value returned by getMonth() representing December |
static CalendarDate |
EARLIEST
The earliest date that can be represented by this class (Januray 1, 1600 A.D.) |
static int |
FEBRUARY
The value returned by getMonth() representing February |
static int |
FRIDAY
The value returned by getDayOfWeek() representing Friday |
static int |
JANUARY
The value returned by getMonth() representing January |
static int |
JULY
The value returned by getMonth() representing July |
static int |
JUNE
The value returned by getMonth() representing June |
static CalendarDate |
LATEST
The latest date that can be represented by this class (December 31, 2999 A.D.) |
static int |
MARCH
The value returned by getMonth() representing March |
static int |
MAY
The value returned by getMonth() representing May |
static int |
MONDAY
The value returned by getDayOfWeek() representing Monday |
static int |
NOVEMBER
The value returned by getMonth() representing November |
static int |
OCTOBER
The value returned by getMonth() representing October |
static int |
SATURDAY
The value returned by getDayOfWeek() representing Saturday |
static int |
SEPTEMBER
The value returned by getMonth() representing September |
static int |
SUNDAY
The value returned by getDayOfWeek() representing Sunday |
static int |
THURSDAY
The value returned by getDayOfWeek() representing Thursday |
static int |
TUESDAY
The value returned by getDayOfWeek() representing Tuesday |
static int |
WEDNESDAY
The value returned by getDayOfWeek() representing Wednesday |
Constructor Summary | |
CalendarDate(int year,
int month,
int dayOfMonth)
Creates a date represented by the given year, month and day. |
|
CalendarDate(java.util.TimeZone tzone)
Creates a CalendarDate representing the current date in the given timezone. |
|
CalendarDate(java.util.TimeZone tzone,
java.util.Date instantInTime)
Creates a CalendarDate representing the date in the given timezone at the given instant in time. |
Method Summary | |
CalendarDate |
addDays(int numDays)
Returns a new date which is this date offset by numDays. |
CalendarDate |
addMonths(int numMonths)
Adds a given number of months to the date while attempting to keep the day of the month. |
int |
compareTo(java.lang.Object other)
If the given object is a CalendarDate: returns less than 0 if this date is before the given date returns 0 if this date is equal to the given date returns more than 0 if this date is after the given date |
static int |
daysInMonth(int year,
int month)
Returns the number of days in the given month. |
int |
daysUntil(CalendarDate otherDay)
Returns the number of days until the given date |
boolean |
equals(java.lang.Object other)
Returns true if the given object is a CalendarDate representing the same date as this object |
int |
getDayOfMonth()
The day of the month |
int |
getDayOfWeek()
The day of the week for this date |
int |
getMonth()
The month of this date |
int |
getYear()
The year of this date |
int |
hashCode()
|
boolean |
isAfter(CalendarDate other)
Returns true if this date is after the given date |
boolean |
isBefore(CalendarDate other)
Returns true if this date is before the given date |
static boolean |
isLeapYear(int year)
Returns true if the year is a leap year in the Gregorian calendar |
static boolean |
isOutsideRange(java.util.TimeZone tzone,
java.util.Date instantInTime)
Returns true if the given instant in time is before EARLIEST or after LATEST in the given timezone. |
int |
monthsUntil(CalendarDate otherDay)
Returns the number of month changes until the given day. |
java.util.Date |
toDate(java.util.TimeZone timezone)
Returns the instant in time when this day begins in the given timezone. |
java.util.Date |
toDate(java.util.TimeZone timezone,
int hour,
int min,
int sec)
Returns the instant in time when the given time of day is reached in the given timezone. |
java.lang.String |
toString()
Returns a string form of this date in the form "2004-9-23" |
Methods inherited from class java.lang.Object |
getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final int SUNDAY
public static final int MONDAY
public static final int TUESDAY
public static final int WEDNESDAY
public static final int THURSDAY
public static final int FRIDAY
public static final int SATURDAY
public static final int JANUARY
public static final int FEBRUARY
public static final int MARCH
public static final int APRIL
public static final int MAY
public static final int JUNE
public static final int JULY
public static final int AUGUST
public static final int SEPTEMBER
public static final int OCTOBER
public static final int NOVEMBER
public static final int DECEMBER
public static final CalendarDate EARLIEST
Note: This date may change to before 1600 in later releases of CalendarDate.
public static final CalendarDate LATEST
Note: This date may change to after 2999 in later releases of CalendarDate.
Constructor Detail |
public CalendarDate(int year, int month, int dayOfMonth)
year
- The year of the date to createmonth
- The month of the date to create (1 = January, 12 = December)dayOfMonth
- The day of the month of the date to create.
java.lang.IllegalArgumentException
- if the year, month and dayOfMonth combination are not valid
in a Gregorian calendar.public CalendarDate(java.util.TimeZone tzone, java.util.Date instantInTime)
Think carefully about what timezone to use! Often you will want
to use the timezone of the 'user' - which is not always represented by
TimeZone.getDefault()
tzone
- The timezone to be consideredinstantInTime
- The instant in time to be considered
java.lang.IllegalArgumentException
- if the instant in time is out of range in the given timezoneisOutsideRange(TimeZone, Date)
public CalendarDate(java.util.TimeZone tzone)
CalendarDate(tzone, new Date())
tzone
- The timezone to be consideredMethod Detail |
public static boolean isOutsideRange(java.util.TimeZone tzone, java.util.Date instantInTime)
public int getDayOfMonth()
public int getDayOfWeek()
public int getMonth()
public int getYear()
public CalendarDate addDays(int numDays)
numDays
- the number of days to be added to this date (can be negative)
java.lang.IllegalArgumentException
- if the resulting day would be before EARLIEST or after
LATEST. That is, if numDays < this.daysUntil(EARLIEST) or
numDays > this.daysUntil(LATEST)public int daysUntil(CalendarDate otherDay)
otherDay
- The date to compare to
public int monthsUntil(CalendarDate otherDay)
otherDay
- The date to compare to
public static boolean isLeapYear(int year)
year
- The year to consider
year
is a leap yearpublic int compareTo(java.lang.Object other)
compareTo
in interface java.lang.Comparable
other
- the date to compare this one to
java.lang.ClassCastException
- if other
is not an instance of CalendarDateComparable.compareTo(java.lang.Object)
public boolean isBefore(CalendarDate other)
other
- The date to consider
public boolean isAfter(CalendarDate other)
other
- The date to consider
public boolean equals(java.lang.Object other)
other
- The date to test against
other
public int hashCode()
public java.lang.String toString()
public java.util.Date toDate(java.util.TimeZone timezone)
timezone
- The timezone to consider
public java.util.Date toDate(java.util.TimeZone timezone, int hour, int min, int sec)
timezone
- The timezone to for which the date applieshour
- The hour of the day in range 0 - 23min
- The minute of the day in range 0 - 59sec
- The second of the day in range 0 - 60 (60 is for leap-seconds)
java.lang.IllegalArgumentException
- if the hour, min or sec parameters are outside the correct
rangepublic static int daysInMonth(int year, int month)
year
- The yearmonth
- The month in range 1 - 12
public CalendarDate addMonths(int numMonths)
new CalendarDate(2005, 12, 31).addMonths(2).addMonths(1)
results in "March 28, 2006"; but,
new CalendarDate(2005, 12, 31).addMonths(3)
results
in "March 31, 2006"
numMonths
- the number of months to be added (can be negative)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |