org.apache.pivot.util
Class CalendarDate

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

public final 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

Nested Class Summary
static class CalendarDate.Range
          Represents a range of calendar dates.
 
Field Summary
 int day
          The day of the month, 0-based.
 int month
          The month field, 0-based.
 int year
          The year field.
 
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.
 
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.
static CalendarDate decode(String value)
          Creates a new date representing the specified date string.
 boolean equals(Object o)
          Indicates whether some other object is "equal to" this one.
static int getMaximumSupportedYear()
          Return the maximum supported year.
static int getMinimumSupportedYear()
          Return the minimum supported year.
 int hashCode()
          Returns a hash code value for the object.
 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.
 GregorianCalendar toCalendar(Time time)
          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
 

Field Detail

year

public final int year
The year field. (e.g. 2008).


month

public final int month
The month field, 0-based. (e.g. 2 for March).


day

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

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)
Method Detail

getMinimumSupportedYear

public static int getMinimumSupportedYear()
Return the minimum supported year. This is to help the org.apache.pivot.wtk.CalendarButton get the right range of years, which should be this value up to and including getMaximumSupportedYear().


getMaximumSupportedYear

public static int getMaximumSupportedYear()
Return the maximum supported year. This is to help the org.apache.pivot.wtk.CalendarButton get the right range of years, which should be getMinimumSupportedYear() up to and including this value.


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 calendar date 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.

toCalendar

public GregorianCalendar toCalendar(Time time)
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.

Parameters:
time - The time of day.
Returns:
This calendar date as a GregorianCalendar.

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.

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

decode

public static CalendarDate decode(String value)
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:
value - A string in the form of [YYYY]-[MM]-[DD] (e.g. 2008-07-23).