org.apache.pivot.util
Class CalendarDate

java.lang.Object
  extended by org.apache.pivot.util.CalendarDate
All Implemented Interfaces:
Serializable, Comparable<CalendarDate>

public class CalendarDate
extends Object
implements Comparable<CalendarDate>, Serializable

CalendarDate allows a specific day to be identified within the gregorian calendar system. This identification has no association with any particular time zone and no notion of the time of day.

See Also:
Serialized Form

Constructor Summary
CalendarDate()
          Creates a new CalendarDate representing the current day in the default timezone and the default locale.
CalendarDate(GregorianCalendar calendar)
          Creates a new CalendarDate representing the day contained in the specified gregorian calendar (assuming the default locale and the default timezone).
CalendarDate(int year, int month, int day)
          Creates a new CalendarDate representing the specified year, month, and day of month.
CalendarDate(String date)
          Creates a new date representing the specified date string.
 
Method Summary
 CalendarDate add(int days)
          Adds the specified number of days to this calendar date and returns the resulting calendar date.
 int compareTo(CalendarDate calendarDate)
          Compares this calendar date with another calendar date.
 boolean equals(Object o)
          Indicates whether some other object is "equal to" this one.
 int getDay()
          Gets the day of the month, 0-based.
 int getMonth()
          Gets the month field, 0-based.
 int getYear()
          Gets the year field.
 int hashCode()
          Returns a hash code value for the object.
 void set(int year, int month, int day)
          Sets the date.
 void setDay(int day)
          Sets the day field.
 void setMonth(int month)
          Sets the month field.
 void setYear(int year)
          Sets the year field.
 int subtract(CalendarDate calendarDate)
          Gets the number of days in between this calendar date and the specified calendar date.
 GregorianCalendar toCalendar()
          Translates this calendar date to an instance of GregorianCalendar, with the year, month, and dayOfMonth fields set in the default time zone with the default locale.
 String toString()
          Returns a string representation of this calendar date in the ISO 8601 "calendar date" format, which is [YYYY]-[MM]-[DD].
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CalendarDate

public CalendarDate()
Creates a new CalendarDate representing the current day in the default timezone and the default locale.


CalendarDate

public CalendarDate(GregorianCalendar calendar)
Creates a new CalendarDate representing the day contained in the specified gregorian calendar (assuming the default locale and the default timezone).

Parameters:
calendar - The calendar containing the year, month, and day fields.

CalendarDate

public CalendarDate(int year,
                    int month,
                    int day)
Creates a new CalendarDate representing the specified year, month, and day of month.

Parameters:
year - The year field. (e.g. 2008)
month - The month field, 0-based. (e.g. 2 for March)
day - The day of the month, 0-based. (e.g. 14 for the 15th)

CalendarDate

public CalendarDate(String date)
Creates a new date representing the specified date string. The date string must be in the ISO 8601 "calendar date" format, which is [YYYY]-[MM]-[DD].

Parameters:
date - A string in the form of [YYYY]-[MM]-[DD]. (e.g. 2008-07-23)
Method Detail

set

public void set(int year,
                int month,
                int day)
Sets the date.

Parameters:
year -
month -
day -

getYear

public int getYear()
Gets the year field. (e.g. 2008).

Returns:
This calendar date's year field

setYear

public void setYear(int year)
Sets the year field.

Parameters:
year -

getMonth

public int getMonth()
Gets the month field, 0-based. (e.g. 2 for March).

Returns:
This calendar date's month field

setMonth

public void setMonth(int month)
Sets the month field.

Parameters:
month -

getDay

public int getDay()
Gets the day of the month, 0-based. (e.g. 14 for the 15th).

Returns:
This calendar date's day field

setDay

public void setDay(int day)
Sets the day field.

Parameters:
day -

compareTo

public int compareTo(CalendarDate calendarDate)
Compares this calendar date with another calendar date.

Specified by:
compareTo in interface Comparable<CalendarDate>
Parameters:
calendarDate - The calendar date against which to compare
Returns:
A negative number, zero, or a positive number if the specified calendar date is less than, equal to, or greater than this calendar date, respectively.

add

public CalendarDate add(int days)
Adds the specified number of days to this calendar date and returns the resulting calendar date. The number of days may be negative, in which case the result will be a date before this calendar date.

More formally, it is defined that given calendar dates c1 and c2, the following will return true:

    c1.add(c2.subtract(c1)).equals(c2);
 

Parameters:
days - The number of days to add to (or subtract from if negative) this calendar date
Returns:
The resulting calendar date

subtract

public int subtract(CalendarDate calendarDate)
Gets the number of days in between this calendar date and the specified calendar date. If this calendar date represents a day after the specified calendar date, the difference will be positive. If this calendardate represents a day before the specified calendar date, the difference will be negative. If the two calendar dates represent the same day, the difference will be zero.

More formally, it is defined that given calendar dates c1 and c2, the following will return true:

    c1.add(c2.subtract(c1)).equals(c2);
 

Parameters:
calendarDate - The calendar date to subtract from this calendar date
Returns:
The number of days in between this calendar date and calendarDate

toCalendar

public GregorianCalendar toCalendar()
Translates this calendar date to an instance of GregorianCalendar, with the year, month, and dayOfMonth fields set in the default time zone with the default locale.

Returns:
This calendar date as a GregorianCalendar

equals

public boolean equals(Object o)
Indicates whether some other object is "equal to" this one. This is the case if the object is a calendar date that represents the same day as this one.

Overrides:
equals in class Object
Parameters:
o - Reference to the object against which to compare

hashCode

public int hashCode()
Returns a hash code value for the object.

Overrides:
hashCode in class Object

toString

public String toString()
Returns a string representation of this calendar date in the ISO 8601 "calendar date" format, which is [YYYY]-[MM]-[DD].

Overrides:
toString in class Object