danger.internal
Class Date

java.lang.Object
  extended by danger.internal.Date


public final class Date
extends Object

Date is a container for a time of day, a month, day, and year, and a time zone.

In general you will want to use java.util.Calendar instead, but you will need to convert to a Date to use DateFormat or DateTimeEditor. When converting between Calendars and Dates (since you still need to use Date for DateFormat), be warned that January is 1 here but 0 in Calendar.

Most of its operations are independent of time zone, and in particular you can set the time of day to a time that does not exist because of a daylight saving transition. The time zone is only used when converting to or from a scalar time representation or when inquiring whether daylight saving is in effect at a particular moment.

The calendar side, in contrast, restricts you to dates that actually exist in the Gregorian calendar. If you try to set the date to January 32nd, you will get February 1st.


Field Summary
static int SECONDS_PER_DAY
           
static int SECONDS_PER_HOUR
           
static int SECONDS_PER_MINUTE
           
 
Constructor Summary
Date()
          Create a new Date in the system time zone and filled in with the current date and time.
Date(Date inDate)
          Create a new Date whose time zone, date, and time are copied from the specified Date.
Date(int inUnixTimeGMT)
          Create a new Date in the system time zone filled in with the date and time corresponding to the specified Unix time (the number of seconds since midnight DST, January 1, 1970).
Date(int inYear, int inMonth, int inDay)
          Create a new Date in the system time zone and filled in with midnight on the specified day.
Date(int inUnixTimeGMT, TimeZone inTZ)
          Create a new Date in the specified time zone, filled in with the date and time corresponding to the specified Unix time (the number of seconds since midnight DST, January 1, 1970).
Date(TimeZone inTZ)
          Create a new Date in the specified time zone and filled in with the current date and time.
 
Method Summary
 void addDays(int inDays)
          Advance the day of the month by the specified number, possibly also affecting the month or year if the change brings the Date into the previous or next month.
 void addHours(int inHours)
          Advance the date and time of this Date by the specified number of hours.
 void addMinutes(int inMinutes)
          Advance the date and time of this Date by the specified number of minutes.
 void addMonths(int inMonths)
          Advance the month of this Date by the specified number.
 void addSeconds(int inSeconds)
          Advance the date and time of this Date by the specified number of seconds.
 void addYears(int inYear)
          Increment the year of this Date by the specified number.
 int compareTo(Date inDate)
          Compares this Date to another Date without regard to their time zones, only to their date and time.
 boolean equals(Date inDate)
          Returns whether this Date contains the same date and time as another date, without regard to their time zones.
 boolean equals(Object obj)
          Returns whether obj is a Date that contains the same date and time as this date, without regard to their time zones.
 String get12HourString()
          Return a String representing the hours component, modulo 12, of this Date's time of day.
 String getAMPMString()
          Return a locale-specific String indicating whether the time of day represented by this Date is before or after noon.
 int getDangerTimeGMT()
          Return the number of seconds since 12:00 am GMT, January 1, 2000, that this Date's day, month, year, and time represent, taking the Date's time zone into account.
 int getDangerTimeLocal()
          Return the number of seconds since 12:00 am GMT, January 1, 2000, that this Date's day, month, year, and time represent, ignoring the Date's time zone and acting as if it were set to GMT.
 int getDay()
          Return the day (1-31) of the month from this Date's date.
 int getDayOfWeek()
          Return the day of the week as a number from 0-6.
 int getDayOfWeekFromSunday()
          Return the day of the week as a number from 0-6 assuming Sunday is 0, and Saturday is 6.
 int getDaysBetween(Date inDate)
          Return the number of days between the specified date and this date.
 int getDaysInMonth()
          Return the number of days in this Date's month.
 String getDayString()
          Return a locale-specific String for the name of the day of the week of this Date.
static String getDayString(int dayOfWeek)
          Return a locale-specific String for the name of the specified day of the week.
 String getEraString()
          Return a locale-specific String indicating whether or not the year represented by this Date is 1 or greater.
 int getHours()
          Return the number of hours (0-23) from this Date's time of day.
 int getMaxDaysInMonth()
          Return the largest number of days that this Date's month can ever have.
 int getMinutes()
          Return the number of minutes (0-59) from this Date's time of day.
 String getMinutesString()
          Return a two-character-long String (padded with a 0 if necessary) representing the minutes component of this Date's time of day.
 int getMonth()
          Return the month (1-12) from this Date's date.
 String getMonthString()
          Return a locale-specific String for the name of the month of this Date.
static String getMonthString(int inMonth)
          Return a locale-specific String for the name of the specified month.
 int getOrdinalDayOfMonth()
          Returns an integer representing the Nth weekday of the month of this date ex.: A date of (2001/4/17) returns 3, since that is the 3rd Tuesday of April
 int getSeconds()
          Return the number of seconds (0-59) from this Date's time of day.
 String getSecondsString()
          Return a two-character-long String (padded with a 0 if necessary) representing the seconds component of this Date's time of day.
 String getShortDayString()
          Return a locale-specific String for the name of the day of the week of this Date.
static String getShortDayString(int dayOfWeek)
          Return a locale-specific String for the name of the specified day of the week.
 String getShortMonthString()
          Return a locale-specific String for the name of the month of this Date.
static String getShortMonthString(int inMonth)
          Return a locale-specific String for the name of the specified month.
 String getShortYearString()
          Return a String representing this Date's year, truncated to at most two digits.
 int getTime()
          Return this Date's time of day.
 TimeZone getTimeZone()
          Return this time zone of this date.
 int getTimeZoneOffset()
          Return the offset from GMT (in seconds) of this Date's time zone at the moment that it expresses.
 int getUnixTimeGMT()
          Return the number of seconds since 12:00 am GMT, January 1, 1970, that this Date's day, month, year, and time represent, taking the Date's time zone into account.
 int getUnixTimeLocal()
          Return the number of seconds since 12:00 am GMT, January 1, 1970, that this Date's day, month, year, and time represent, ignoring the Date's time zone and acting as if it were set to GMT.
 int getYear()
          Return the year from this Date's date.
 int hashCode()
           
 boolean inDaylightTime()
          Returns whether the date should be in daylight saving time.
 boolean isLeapYear()
          Return whether this Date's year is a leap year.
 boolean isSameDay(Date inDate)
          Returns whether this Date is the same day (but possibly at a different time) as the specified Date.
 void set(Date inDate)
          Set the time zone, date, and time of this Date to match those of the specified Date.
 void set(int inYear, int inMonth, int inDay)
          Set the day, month, and year of this Date to match the specified values, and set the time to midnight (of this Date's time zone).
 void setDangerTimeGMT(int inTime)
          Set the day, month, year, and time of this Date to correspond to the specified number of seconds since 12:00 am GMT, January 1, 2000, taking the Date's time zone into account.
 void setDangerTimeLocal(int inRefTime)
          Set the day, month, year, and time of this Date to correspond to the specified number of seconds since 12:00 am GMT, January 1, 2000, ignoring the Date's time zone and acting as if it were set to GMT.
 void setDay(int inDay)
          Set the day of the month to the specified number.
 void setHours(int inHours)
          Set the hours component of this Date's time to the specified value.
 void setLocale(String s)
          Sets the locale for DateFormats derived from this Date.
 void setMinutes(int inMinutes)
          Set the minutes component of this Date's time to the specified value (0-59).
 void setMonth(int inMonth)
          Set the month of this Date to the specified number.
 void setSeconds(int inSeconds)
          Set the seconds component of this Date's time to the specified value (0-59).
 void setTime(int inTime)
          Sets the time of day.
 void setTime(int inHours, int inMinutes, int inSeconds)
          Set the time of day of this Date to the specified hours, minutes, and seconds.
 void setTimeZone(TimeZone inTZ)
          Set the time zone of this date.
 void setToday()
          Set the day, month, year, and time of this Date to match those returned by the hardware clock.
 void setToday(TimeZone inTZ)
          Set the time zone of this Date to the specified zone and then fill in the day, month, year, and time with those returned by the hardware clock.
 void setUnixTimeGMT(int inTime)
          Set the day, month, year, and time of this Date to correspond to the specified number of seconds since 12:00 am GMT, January 1, 1970, taking the Date's time zone into account.
 void setUnixTimeLocal(int inRefTime)
          Set the day, month, year, and time of this Date to correspond to the specified number of seconds since 12:00 am GMT, January 1, 1970, ignoring the Date's time zone and acting as if it were set to GMT.
 void setYear(int inYear)
          Set the year of this Date to the specified number.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SECONDS_PER_MINUTE

public static final int SECONDS_PER_MINUTE
See Also:
Constant Field Values

SECONDS_PER_HOUR

public static final int SECONDS_PER_HOUR
See Also:
Constant Field Values

SECONDS_PER_DAY

public static final int SECONDS_PER_DAY
See Also:
Constant Field Values
Constructor Detail

Date

public Date()
Create a new Date in the system time zone and filled in with the current date and time.


Date

public Date(TimeZone inTZ)
Create a new Date in the specified time zone and filled in with the current date and time.


Date

public Date(int inYear,
            int inMonth,
            int inDay)
Create a new Date in the system time zone and filled in with midnight on the specified day.


Date

public Date(int inUnixTimeGMT)
Create a new Date in the system time zone filled in with the date and time corresponding to the specified Unix time (the number of seconds since midnight DST, January 1, 1970). This uses the POSIX interpretation of Unix time, where every day contains exactly 86400 seconds.


Date

public Date(int inUnixTimeGMT,
            TimeZone inTZ)
Create a new Date in the specified time zone, filled in with the date and time corresponding to the specified Unix time (the number of seconds since midnight DST, January 1, 1970). This uses the POSIX interpretation of Unix time, where every day contains exactly 86400 seconds.


Date

public Date(Date inDate)
Create a new Date whose time zone, date, and time are copied from the specified Date.

Method Detail

set

public void set(Date inDate)
Set the time zone, date, and time of this Date to match those of the specified Date.


set

public void set(int inYear,
                int inMonth,
                int inDay)
Set the day, month, and year of this Date to match the specified values, and set the time to midnight (of this Date's time zone).


setTimeZone

public void setTimeZone(TimeZone inTZ)
Set the time zone of this date. The day, month, year, and time are unchanged, so the Date now refers to a different absolute time than it did before.


getTimeZone

public TimeZone getTimeZone()
Return this time zone of this date.


getTimeZoneOffset

public int getTimeZoneOffset()
Return the offset from GMT (in seconds) of this Date's time zone at the moment that it expresses. Note that there is an ambiguity at the "fall back" DST transition and that you are better off asking for the offset based upon scalar time if possible.


setToday

public void setToday()
Set the day, month, year, and time of this Date to match those returned by the hardware clock.


setToday

public void setToday(TimeZone inTZ)
Set the time zone of this Date to the specified zone and then fill in the day, month, year, and time with those returned by the hardware clock.


setDangerTimeGMT

public void setDangerTimeGMT(int inTime)
Set the day, month, year, and time of this Date to correspond to the specified number of seconds since 12:00 am GMT, January 1, 2000, taking the Date's time zone into account.

So if the time zone is set to GMT, 0 will yield 12:00 am, January 1, 2000, while if the time zone is set to Pacific time, 0 will yield 4:00 pm, December 31, 1999.


getDangerTimeGMT

public int getDangerTimeGMT()
Return the number of seconds since 12:00 am GMT, January 1, 2000, that this Date's day, month, year, and time represent, taking the Date's time zone into account.

So if the date contains 12:00 am, January 1, 2000, if the time zone is set to GMT the return value will be 0, but if the time zone is set to Pacific time, the return value will be 28800.

Note: Dates prior to 12/13/1931 or after 1/19/2068 can not properly be returned by this method, as the number of seconds can not be represented by an integer. In this case, this method will throw a DateOutOfRangeException.


setDangerTimeLocal

public void setDangerTimeLocal(int inRefTime)
Set the day, month, year, and time of this Date to correspond to the specified number of seconds since 12:00 am GMT, January 1, 2000, ignoring the Date's time zone and acting as if it were set to GMT.

So 0 will yield 12:00 am, January 1, 2000 no matter what the time zone is set to.


getDangerTimeLocal

public int getDangerTimeLocal()
Return the number of seconds since 12:00 am GMT, January 1, 2000, that this Date's day, month, year, and time represent, ignoring the Date's time zone and acting as if it were set to GMT.

So if the date contains 12:00 am, January 1, 2000, the return value will be 0 no matter what the time zone is set to.

Note: Dates prior to 12/13/1931 or after 1/19/2068 can not properly be returned by this method, as the number of seconds can not be represented by an integer. In this case, this method will throw a DateOutOfRangeException.


setUnixTimeGMT

public void setUnixTimeGMT(int inTime)
Set the day, month, year, and time of this Date to correspond to the specified number of seconds since 12:00 am GMT, January 1, 1970, taking the Date's time zone into account.

So if the time zone is set to GMT, 0 will yield 12:00 am, January 1, 1970, while if the time zone is set to Pacific time, 0 will yield 4:00 pm, December 31, 1969.


getUnixTimeGMT

public int getUnixTimeGMT()
Return the number of seconds since 12:00 am GMT, January 1, 1970, that this Date's day, month, year, and time represent, taking the Date's time zone into account.

So if the date contains 12:00 am, January 1, 1970, if the time zone is set to GMT the return value will be 0, but if the time zone is set to Pacific time, the return value will be 28800.

Note: Dates prior to 12/14/1901 or after 1/18/2038 can not properly be returned by this method, as the number of seconds can not be represented by an integer. In this case, this method will throw a DateOutOfRangeException.


setUnixTimeLocal

public void setUnixTimeLocal(int inRefTime)
Set the day, month, year, and time of this Date to correspond to the specified number of seconds since 12:00 am GMT, January 1, 1970, ignoring the Date's time zone and acting as if it were set to GMT.

So 0 will yield 12:00 am, January 1, 1970 no matter what the time zone is set to.


getUnixTimeLocal

public int getUnixTimeLocal()
Return the number of seconds since 12:00 am GMT, January 1, 1970, that this Date's day, month, year, and time represent, ignoring the Date's time zone and acting as if it were set to GMT.

So if the date contains 12:00 am, January 1, 1970, the return value will be 0 no matter what the time zone is set to.

Note: Dates prior to 12/14/1901 or after 1/18/2038 can not properly be returned by this method, as the number of seconds can not be represented by an integer. In this case, this method will throw a DateOutOfRangeException.


setLocale

public void setLocale(String s)
Sets the locale for DateFormats derived from this Date. You normally won't need to call this because the default locale will be the user's locale.


inDaylightTime

public boolean inDaylightTime()
Returns whether the date should be in daylight saving time. If this date is within the one hour window during switching from Daylight to Standard time, it is impossible to tell which value to return. Therefore, TimeZone.inDaylightTime(long inUnixTimeGMT) is preferred.


isSameDay

public boolean isSameDay(Date inDate)
Returns whether this Date is the same day (but possibly at a different time) as the specified Date.

Since:
4.5

equals

public boolean equals(Date inDate)
Returns whether this Date contains the same date and time as another date, without regard to their time zones.


equals

public boolean equals(Object obj)
Returns whether obj is a Date that contains the same date and time as this date, without regard to their time zones.

Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

compareTo

public int compareTo(Date inDate)
Compares this Date to another Date without regard to their time zones, only to their date and time.


getSeconds

public int getSeconds()
Return the number of seconds (0-59) from this Date's time of day.


getMinutes

public int getMinutes()
Return the number of minutes (0-59) from this Date's time of day.


getHours

public int getHours()
Return the number of hours (0-23) from this Date's time of day.


getDay

public int getDay()
Return the day (1-31) of the month from this Date's date.


getMonth

public int getMonth()
Return the month (1-12) from this Date's date. Note that January is 1, not 0, unlike in Calendar.


getYear

public int getYear()
Return the year from this Date's date.


getTime

public int getTime()
Return this Date's time of day. Note that this is the time as presented to the user, so on the day of a daylight saving transition there is an hour that happens twice or does not happen at all; noon is always 43200 even if there have actually been 3600 seconds fewer or more before noon that day.


setSeconds

public void setSeconds(int inSeconds)
Set the seconds component of this Date's time to the specified value (0-59). If you pass an out-of-range value, it will advance or retract other components of the date or time to match the extent by which the seconds are out of range. However, in doing so it does not take daylight saving into account, so one second past 1:59:59 at the start of summer is 2:00:00, not 3:00:00.


addSeconds

public void addSeconds(int inSeconds)
Advance the date and time of this Date by the specified number of seconds. However, in doing so it does not take daylight saving into account, so one second past 1:59:59 at the start of summer is 2:00:00, not 3:00:00.


setMinutes

public void setMinutes(int inMinutes)
Set the minutes component of this Date's time to the specified value (0-59). If you pass an out-of-range value, it will advance or retract other components of the date or time to match the extent by which the seconds are out of range. However, in doing so it does not take daylight saving into account, so one minute past 1:59:00 at the start of summer is 2:00:00, not 3:00:00.


addMinutes

public void addMinutes(int inMinutes)
Advance the date and time of this Date by the specified number of minutes. However, in doing so it does not take daylight saving into account, so one minute past 1:59:00 at the start of summer is 2:00:00, not 3:00:00.


setHours

public void setHours(int inHours)
Set the hours component of this Date's time to the specified value. It does not take daylight saving into account, so you can set the time to 2:00:00 even on the day when daylight saving begins. If you pass an out-of-range value, it will advance or retract the date to bring the hours into range.


addHours

public void addHours(int inHours)
Advance the date and time of this Date by the specified number of hours. However, in doing so it does not take daylight saving into account, so one hour past 1:00:00 at the start of summer is 2:00:00, not 3:00:00.


setDay

public void setDay(int inDay)
Set the day of the month to the specified number. If you pass an out-of-range value, the month and year will be adjusted to bring the day into range.


addDays

public void addDays(int inDays)
Advance the day of the month by the specified number, possibly also affecting the month or year if the change brings the Date into the previous or next month.


setMonth

public void setMonth(int inMonth)
Set the month of this Date to the specified number. January is 1, not 0 as in Calendar. If you specify an out-of-range value, it will adjust the year as well to bring the months into range. If the day of the month becomes out of range through setting the month, it will be truncated back into range (January 31st set to February will yield February 28th).


addMonths

public void addMonths(int inMonths)
Advance the month of this Date by the specified number. If the change would go past the start or end of the year, the year will be changed as well. If the day of the month becomes out of range through setting the month, it will be truncated back into range (January 31st set to February will yield February 28th).


setYear

public void setYear(int inYear)
Set the year of this Date to the specified number. If the day of the month goes out of range due to a leap year difference, it will be truncated back into range (February 29, 2004 changed to 2005 will yield February 28, 2005).


addYears

public void addYears(int inYear)
Increment the year of this Date by the specified number. If the day of the month goes out of range due to a leap year difference, it will be truncated back into range (February 29, 2004 changed to 2005 will yield February 28, 2005).


setTime

public void setTime(int inHours,
                    int inMinutes,
                    int inSeconds)
Set the time of day of this Date to the specified hours, minutes, and seconds. If you specify an out of range value, the days, months, and years will be adjusted to bring the time back into range. Daylight saving is not taken into account, so it will let you set the time to 2:00:00 even on the day when daylight saving begins.


setTime

public void setTime(int inTime)
Sets the time of day. The argument is seconds + 60 * minutes + 360 * seconds. If you specify an out of range value, the days, months, and years will be adjusted to bring the time back into range. The argument is user-visible time, not actual seconds since midnight, so 43200 is always noon even if this day is the start or end of daylight saving and there have actually been 3600 seconds more or fewer before noon.


isLeapYear

public boolean isLeapYear()
Return whether this Date's year is a leap year.


getSecondsString

public String getSecondsString()
Return a two-character-long String (padded with a 0 if necessary) representing the seconds component of this Date's time of day.


getMinutesString

public String getMinutesString()
Return a two-character-long String (padded with a 0 if necessary) representing the minutes component of this Date's time of day.


get12HourString

public String get12HourString()
Return a String representing the hours component, modulo 12, of this Date's time of day.


getAMPMString

public String getAMPMString()
Return a locale-specific String indicating whether the time of day represented by this Date is before or after noon.


getEraString

public String getEraString()
Return a locale-specific String indicating whether or not the year represented by this Date is 1 or greater.


getMonthString

public String getMonthString()
Return a locale-specific String for the name of the month of this Date.


getMonthString

public static String getMonthString(int inMonth)
Return a locale-specific String for the name of the specified month.


getShortMonthString

public String getShortMonthString()
Return a locale-specific String for the name of the month of this Date.


getShortMonthString

public static String getShortMonthString(int inMonth)
Return a locale-specific String for the name of the specified month.


getDayString

public String getDayString()
Return a locale-specific String for the name of the day of the week of this Date.


getDayString

public static String getDayString(int dayOfWeek)
Return a locale-specific String for the name of the specified day of the week. Note that which day is 1 is different in different locales.


getShortDayString

public String getShortDayString()
Return a locale-specific String for the name of the day of the week of this Date.


getShortDayString

public static String getShortDayString(int dayOfWeek)
Return a locale-specific String for the name of the specified day of the week. Note that which day is 1 is different in different locales.


getShortYearString

public String getShortYearString()
Return a String representing this Date's year, truncated to at most two digits.


getDayOfWeek

public int getDayOfWeek()
Return the day of the week as a number from 0-6. Which day is 0 depends upon the locale.


getDayOfWeekFromSunday

public int getDayOfWeekFromSunday()
Return the day of the week as a number from 0-6 assuming Sunday is 0, and Saturday is 6.


getDaysBetween

public int getDaysBetween(Date inDate)
Return the number of days between the specified date and this date.


getDaysInMonth

public int getDaysInMonth()
Return the number of days in this Date's month.


getMaxDaysInMonth

public int getMaxDaysInMonth()
Return the largest number of days that this Date's month can ever have.


getOrdinalDayOfMonth

public int getOrdinalDayOfMonth()
Returns an integer representing the Nth weekday of the month of this date ex.: A date of (2001/4/17) returns 3, since that is the 3rd Tuesday of April