Class CalendarDate
- java.lang.Object
-
- org.apache.pivot.util.CalendarDate
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Comparable<CalendarDate>
public final class CalendarDate extends java.lang.Object implements java.lang.Comparable<CalendarDate>, java.io.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, except that conversions to/from Gregorian calendar may be affected by time zone, so the zone may be set during construction if desired, and the time of day can also be set for such conversions.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
CalendarDate.Range
Represents a range of calendar dates.
-
Field Summary
Fields Modifier and Type Field Description int
day
The day of the month, 0-based.static int
MAX_CALENDAR_YEAR
Maximum supported year (must be less or equal).static Time
MIDNIGHT_TIME
The midnight time of dayTime(0,0,0)
.static long
MILLIS_PER_DAY
The number of milliseconds in a day.static int
MIN_CALENDAR_YEAR
Minimum supported year (must be greater or equal).int
month
The month field, 0-based.java.util.TimeZone
timeZone
TheTimeZone
associated with this calendar date (if any).static java.util.TimeZone
TIMEZONE_GMT
The timezone of the (universal) GMT zone (at the prime meridian).int
year
The year field.
-
Constructor Summary
Constructors Constructor Description CalendarDate()
Creates a newCalendarDate
representing the current day in the default timezone and the default locale.CalendarDate(int year, int month, int day)
Creates a newCalendarDate
representing the specified year, month, and day of month.CalendarDate(int year, int month, int day, java.util.TimeZone timeZone)
Creates a newCalendarDate
representing the specified year, month, day of month, and timezone.CalendarDate(java.time.LocalDate localDate)
Creates a newCalendarDate
from the givenLocalDate
(new in Java 8).CalendarDate(java.util.GregorianCalendar calendar)
Creates a newCalendarDate
representing the day contained in the specified Gregorian calendar (assuming the default locale and the default timezone).CalendarDate(java.util.TimeZone timeZone)
Creates a newCalendarDate
representing the current day in the given timezone and the default locale.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description CalendarDate
add(int days)
Adds the specified number of days to this calendar date and returns the resulting calendar date.CalendarDate
addMonths(int months)
Adds the specified number of months to this calendar date and returns the resulting calendar date.CalendarDate
addYears(int years)
Adds the specified number of years 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(java.lang.String value)
Creates a new date representing the specified date string.boolean
equals(java.lang.Object o)
Indicates whether some other object is "equal to" this one.int
hashCode()
Returns a hash code value for the object.static boolean
isLeapYear(int year)
int
subtract(CalendarDate calendarDate)
Gets the number of days in between this calendar date and the specified calendar date.java.util.GregorianCalendar
toCalendar()
Translates this calendar date to an instance ofGregorianCalendar
, with theyear
,month
, anddayOfMonth
fields set in the time zone set at construction with the default locale.java.util.GregorianCalendar
toCalendar(Time time)
Translates this calendar date along with the given time of day to an instance ofGregorianCalendar
, with theyear
,month
, anddayOfMonth
fields set in the time zone set at construction with the default locale.java.time.LocalDate
toLocalDate()
java.time.LocalDateTime
toLocalDateTime(Time time)
java.lang.String
toString()
Returns a string representation of this calendar date in theISO 8601
"calendar date" format, which is[YYYY]-[MM]-[DD]
.
-
-
-
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).
-
timeZone
public final java.util.TimeZone timeZone
TheTimeZone
associated with this calendar date (if any).By default this is not set, which means using the local timezone.
This is only meaningful for conversions to/from Gregorian calendar (and implicitly then for difference calculations which use a calculated calendar value). Can be set in the non-default constructor (for instance to GMT for the
CalendarDateSpinnerData
class).Note: difference calculations will only be affected by the timezone if the two dates are in different ones, otherwise the calculations will be the same no matter what zone is used.
-
MIN_CALENDAR_YEAR
public static final int MIN_CALENDAR_YEAR
Minimum supported year (must be greater or equal).- See Also:
- Constant Field Values
-
MAX_CALENDAR_YEAR
public static final int MAX_CALENDAR_YEAR
Maximum supported year (must be less or equal).- See Also:
- Constant Field Values
-
MILLIS_PER_DAY
public static final long MILLIS_PER_DAY
The number of milliseconds in a day. Used by various pieces of code.- See Also:
- Constant Field Values
-
TIMEZONE_GMT
public static final java.util.TimeZone TIMEZONE_GMT
The timezone of the (universal) GMT zone (at the prime meridian).
-
MIDNIGHT_TIME
public static final Time MIDNIGHT_TIME
The midnight time of dayTime(0,0,0)
.
-
-
Constructor Detail
-
CalendarDate
public CalendarDate()
Creates a newCalendarDate
representing the current day in the default timezone and the default locale.
-
CalendarDate
public CalendarDate(java.util.TimeZone timeZone)
Creates a newCalendarDate
representing the current day in the given timezone and the default locale.- Parameters:
timeZone
- The timezone to use for this calendar date.
-
CalendarDate
public CalendarDate(java.util.GregorianCalendar calendar)
Creates a newCalendarDate
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(java.time.LocalDate localDate)
Creates a newCalendarDate
from the givenLocalDate
(new in Java 8). This does not represent a moment in time, but only represents a date (as in year, month and day).- Parameters:
localDate
- The date value containing year, month and day fields.
-
CalendarDate
public CalendarDate(int year, int month, int day)
Creates a newCalendarDate
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)- See Also:
MIN_CALENDAR_YEAR
,MAX_CALENDAR_YEAR
-
CalendarDate
public CalendarDate(int year, int month, int day, java.util.TimeZone timeZone)
Creates a newCalendarDate
representing the specified year, month, day of month, and timezone.- 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)timeZone
- The timezone to assume for conversions and differences (ifnull
then the default TimeZone will be used).- See Also:
MIN_CALENDAR_YEAR
,MAX_CALENDAR_YEAR
-
-
Method Detail
-
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
andc2
, the following will returntrue
: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.
-
addMonths
public CalendarDate addMonths(int months)
Adds the specified number of months to this calendar date and returns the resulting calendar date. The number of months 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
andc2
, the following will returntrue
:c1.add(c2.subtract(c1)).equals(c2);
- Parameters:
months
- The number of months to add to (or subtract from if negative) this calendar date.- Returns:
- The resulting calendar date.
-
addYears
public CalendarDate addYears(int years)
Adds the specified number of years to this calendar date and returns the resulting calendar date. The number of years 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
andc2
, the following will returntrue
:c1.add(c2.subtract(c1)).equals(c2);
- Parameters:
years
- The number of years 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
andc2
, the following will returntrue
: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 java.util.GregorianCalendar toCalendar()
Translates this calendar date to an instance ofGregorianCalendar
, with theyear
,month
, anddayOfMonth
fields set in the time zone set at construction with the default locale.- Returns:
- This calendar date as a
GregorianCalendar
.
-
toCalendar
public java.util.GregorianCalendar toCalendar(Time time)
Translates this calendar date along with the given time of day to an instance ofGregorianCalendar
, with theyear
,month
, anddayOfMonth
fields set in the time zone set at construction with the default locale.- Parameters:
time
- The time of day.- Returns:
- This calendar date as a
GregorianCalendar
.
-
toLocalDate
public java.time.LocalDate toLocalDate()
- Returns:
- An equivalent
LocalDate
that represents the same calendar date as this date.
-
toLocalDateTime
public java.time.LocalDateTime toLocalDateTime(Time time)
- Parameters:
time
- The wall clock time to combine with this calendar date.- Returns:
- A date and time representing this calendar date along with the given wall clock time.
-
compareTo
public int compareTo(CalendarDate calendarDate)
Compares this calendar date with another calendar date.- Specified by:
compareTo
in interfacejava.lang.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(java.lang.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 classjava.lang.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 classjava.lang.Object
-
toString
public java.lang.String toString()
Returns a string representation of this calendar date in theISO 8601
"calendar date" format, which is[YYYY]-[MM]-[DD]
.- Overrides:
toString
in classjava.lang.Object
-
isLeapYear
public static boolean isLeapYear(int year)
- Parameters:
year
- The year to check.- Returns:
- Is the given year a leap year according to the standard definition?
-
decode
public static CalendarDate decode(java.lang.String value)
Creates a new date representing the specified date string. The date string must be in theISO 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).- Returns:
- The
CalendarDate
corresponding to the input string.
-
-