java.util

Class Date

public class Date extends Object implements Cloneable, Comparable<Date>, Serializable

This class represents a specific time in milliseconds since the epoch. The epoch is 1970, January 1 00:00:00.0000 UTC.

Date is intended to reflect universal time coordinate (UTC), but this depends on the underlying host environment. Most operating systems don't handle the leap second, which occurs about once every year or so. The leap second is added to the last minute of the day on either the 30th of June or the 31st of December, creating a minute 61 seconds in length.

The representations of the date fields are as follows:

Prior to JDK 1.1, this class was the sole class handling date and time related functionality. However, this particular solution was not amenable to internationalization. The new Calendar class should now be used to handle dates and times, with Date being used only for values in milliseconds since the epoch. The Calendar class, and its concrete implementations, handle the interpretation of these values into minutes, hours, days, months and years. The formatting and parsing of dates is left to the DateFormat class, which is able to handle the different types of date format which occur in different locales.

See Also: Calendar GregorianCalendar DateFormat

Constructor Summary
Date()
Creates a new Date Object representing the current time.
Date(long time)
Creates a new Date Object representing the given time.
Date(int year, int month, int day)
Creates a new Date Object representing the given time.
Date(int year, int month, int day, int hour, int min)
Creates a new Date Object representing the given time.
Date(int year, int month, int day, int hour, int min, int sec)
Creates a new Date Object representing the given time.
Date(String s)
Creates a new Date from the given string representation.
Method Summary
booleanafter(Date when)
Tests if this date is after the specified date.
booleanbefore(Date when)
Tests if this date is before the specified date.
Objectclone()
Returns a copy of this Date object.
intcompareTo(Date when)
Compares two dates.
booleanequals(Object obj)
Compares two dates for equality.
intgetDate()
Returns the day of the month of this Date object, as a value between 0 and 31.
intgetDay()
Returns the day represented by this Date object as an integer between 0 (Sunday) and 6 (Saturday).
intgetHours()
Returns the hours represented by this Date object as an integer between 0 and 23.
intgetMinutes()
Returns the number of minutes represented by the Date object, as an integer between 0 and 59.
intgetMonth()
Returns the month represented by this Date object, as a value between 0 (January) and 11 (December).
intgetSeconds()
Returns the number of seconds represented by the Date object, as an integer between 0 and 61 (60 and 61 being leap seconds).
longgetTime()
Gets the time represented by this object.
intgetTimezoneOffset()
Returns the number of minutes offset used with UTC to give the time represented by this object in the current time zone.
intgetYear()
Returns the difference between the year represented by this Date object and 1900.
inthashCode()
Computes the hash code of this Date as the XOR of the most significant and the least significant 32 bits of the 64 bit milliseconds value.
static longparse(String string)

Parses a String and returns the time, in milliseconds since the epoch, it represents.

voidsetDate(int date)
Sets the date to the given value.
voidsetHours(int hours)
Sets the hours to the given value.
voidsetMinutes(int minutes)
Sets the minutes to the given value.
voidsetMonth(int month)
Sets the month to the given value.
voidsetSeconds(int seconds)
Sets the seconds to the given value.
voidsetTime(long time)
Sets the time which this object should represent.
voidsetYear(int year)
Sets the year to the specified year, plus 1900.
StringtoGMTString()

Returns a string representation of this Date object using GMT rather than the local timezone.

StringtoLocaleString()
Returns a locale-dependent string representation of this Date object.
StringtoString()

Returns a string representation of this date using the following date format:

day mon dd hh:mm:ss zz yyyy

where the fields used here are:

  • day -- the day of the week (Sunday through to Saturday).
static longUTC(int year, int month, int date, int hrs, int min, int sec)
Returns the number of milliseconds since the epoch specified by the given arguments.

Constructor Detail

Date

public Date()
Creates a new Date Object representing the current time.

Date

public Date(long time)
Creates a new Date Object representing the given time.

Parameters: time the time in milliseconds since the epoch.

Date

public Date(int year, int month, int day)

Deprecated: use new GregorianCalendar(year+1900, month, day) instead.

Creates a new Date Object representing the given time.

Parameters: year the difference between the required year and 1900. month the month as a value between 0 and 11. day the day as a value between 0 and 31.

Date

public Date(int year, int month, int day, int hour, int min)

Deprecated: use new GregorianCalendar(year+1900, month, day, hour, min) instead.

Creates a new Date Object representing the given time.

Parameters: year the difference between the required year and 1900. month the month as a value between 0 and 11. day the day as a value between 0 and 31. hour the hour as a value between 0 and 23, in 24-hour clock notation. min the minute as a value between 0 and 59.

Date

public Date(int year, int month, int day, int hour, int min, int sec)

Deprecated: use new GregorianCalendar(year+1900, month, day, hour, min, sec) instead.

Creates a new Date Object representing the given time.

Parameters: year the difference between the required year and 1900. month the month as a value between 0 and 11. day the day as a value between 0 and 31. hour the hour as a value between 0 and 23, in 24-hour clock notation. min the minute as a value between 0 and 59. sec the second as a value between 0 and 61 (with 60 and 61 being leap seconds).

Date

public Date(String s)

Deprecated: use java.text.DateFormat.parse(s) instead.

Creates a new Date from the given string representation. This does the same as new Date(Date.parse(s))

See Also: Date

Method Detail

after

public boolean after(Date when)
Tests if this date is after the specified date.

Parameters: when the other date

Returns: true, if the date represented by this object is strictly later than the time represented by when.

before

public boolean before(Date when)
Tests if this date is before the specified date.

Parameters: when the other date

Returns: true, if the date represented by when is strictly later than the time represented by this object.

clone

public Object clone()
Returns a copy of this Date object.

Returns: a copy, or null if the object couldn't be cloned.

See Also: clone

compareTo

public int compareTo(Date when)
Compares two dates.

Parameters: when the other date.

Returns: 0, if the date represented by obj is exactly the same as the time represented by this object, a negative if this Date is before the other Date, and a positive value otherwise.

equals

public boolean equals(Object obj)
Compares two dates for equality.

Parameters: obj the object to compare.

Returns: true, if obj is a Date object and the time represented by obj is exactly the same as the time represented by this object.

getDate

public int getDate()

Deprecated: Use Calendar instead of Date, and use get(Calendar.DATE) instead.

Returns the day of the month of this Date object, as a value between 0 and 31.

Returns: the day of month represented by this date object.

See Also: Calendar Date

getDay

public int getDay()

Deprecated: Use Calendar instead of Date, and use get(Calendar.DAY_OF_WEEK) instead.

Returns the day represented by this Date object as an integer between 0 (Sunday) and 6 (Saturday).

Returns: the day represented by this date object.

See Also: Calendar

getHours

public int getHours()

Deprecated: Use Calendar instead of Date, and use get(Calendar.HOUR_OF_DAY) instead.

Returns the hours represented by this Date object as an integer between 0 and 23.

Returns: the hours represented by this date object.

See Also: Calendar Date

getMinutes

public int getMinutes()

Deprecated: Use Calendar instead of Date, and use get(Calendar.MINUTE) instead.

Returns the number of minutes represented by the Date object, as an integer between 0 and 59.

Returns: the minutes represented by this date object.

See Also: Calendar Date

getMonth

public int getMonth()

Deprecated: Use Calendar instead of Date, and use get(Calendar.MONTH) instead.

Returns the month represented by this Date object, as a value between 0 (January) and 11 (December).

Returns: the month represented by this date object (zero based).

See Also: Date Calendar

getSeconds

public int getSeconds()

Deprecated: Use Calendar instead of Date, and use get(Calendar.SECOND) instead.

Returns the number of seconds represented by the Date object, as an integer between 0 and 61 (60 and 61 being leap seconds).

Returns: the seconds represented by this date object.

See Also: Calendar Date

getTime

public long getTime()
Gets the time represented by this object.

Returns: the time in milliseconds since the epoch.

getTimezoneOffset

public int getTimezoneOffset()

Deprecated: use Calendar.get(Calendar.ZONE_OFFSET)+Calendar.get(Calendar.DST_OFFSET) instead.

Returns the number of minutes offset used with UTC to give the time represented by this object in the current time zone. The date information from this object is also used to determine whether or not daylight savings time is in effect. For example, the offset for the UK would be 0 if the month of the date object was January, and 1 if the month was August.

Returns: The time zone offset in minutes of the local time zone relative to UTC. The time represented by this object is used to determine if we should use daylight savings.

getYear

public int getYear()

Deprecated: Use Calendar instead of Date, and use get(Calendar.YEAR) instead. Note the 1900 difference in the year.

Returns the difference between the year represented by this Date object and 1900.

Returns: the year minus 1900 represented by this date object.

See Also: Calendar Date

hashCode

public int hashCode()
Computes the hash code of this Date as the XOR of the most significant and the least significant 32 bits of the 64 bit milliseconds value.

Returns: the hash code.

parse

public static long parse(String string)

Deprecated: Use DateFormat.parse(String)

Parses a String and returns the time, in milliseconds since the epoch, it represents. Most syntaxes are handled, including the IETF date standard "day, dd mon yyyy hh:mm:ss zz" (see toString() for definitions of these fields). Standard U.S. time zone abbreviations are recognised, in addition to time zone offsets in positive or negative minutes. If a time zone is specified, the specified time is assumed to be in UTC and the appropriate conversion is applied, following parsing, to convert this to the local time zone. If no zone is specified, the time is assumed to already be in the local time zone.

The method parses the string progressively from left to right. At the end of the parsing process, either a time is returned or an IllegalArgumentException is thrown to signify failure. The ASCII characters A-Z, a-z, 0-9, and ',', '+', '-', ':' and '/' are the only characters permitted within the string, besides whitespace and characters enclosed within parantheses '(' and ')'.

A sequence of consecutive digits are recognised as a number, and interpreted as follows:

A sequence of consecutive alphabetic characters is recognised as a word, and interpreted as follows, in a case-insentive fashion:

Parameters: string The String to parse.

Returns: The time in milliseconds since the epoch.

Throws: IllegalArgumentException if the string fails to parse.

See Also: toString SimpleDateFormat

setDate

public void setDate(int date)

Deprecated: Use Calendar instead of Date, and use set(Calendar.DATE, date) instead.

Sets the date to the given value. The other fields are only altered as necessary to match the same date and time on the new day of the month. In most cases, the other fields won't change at all. However, in the case of a leap second or the day being out of the range of the current month, values may be adjusted. For example, if the day of the month is currently 30 and the month is June, a new day of the month value of 31 will cause the month to change to July, as June only has 30 days . Similarly, a seconds value of 60 or 61 (a leap second) may result in the seconds value being reset to 0 and the minutes value being incremented by 1, if the new time does not include a leap second.

Parameters: date the date.

See Also: Calendar getDate

setHours

public void setHours(int hours)

Deprecated: Use Calendar instead of Date, and use set(Calendar.HOUR_OF_DAY, hours) instead.

Sets the hours to the given value. The other fields are only altered as necessary to match the same date and time in the new hour. In most cases, the other fields won't change at all. However, in the case of a leap second, values may be adjusted. For example, a seconds value of 60 or 61 (a leap second) may result in the seconds value being reset to 0 and the minutes value being incremented by 1 if the new hour does not contain a leap second.

Parameters: hours the hours.

See Also: Calendar getHours

setMinutes

public void setMinutes(int minutes)

Deprecated: Use Calendar instead of Date, and use set(Calendar.MINUTE, minutes) instead.

Sets the minutes to the given value. The other fields are only altered as necessary to match the same date and time in the new minute. In most cases, the other fields won't change at all. However, in the case of a leap second, values may be adjusted. For example, a seconds value of 60 or 61 (a leap second) may result in the seconds value being reset to 0 and the minutes value being incremented by 1 if the new minute does not contain a leap second.

Parameters: minutes the minutes.

See Also: Calendar getMinutes

setMonth

public void setMonth(int month)

Deprecated: Use Calendar instead of Date, and use set(Calendar.MONTH, month) instead.

Sets the month to the given value. The other fields are only altered as necessary to match the same date and time in the new month. In most cases, the other fields won't change at all. However, in the case of a shorter month or a leap second, values may be adjusted. For example, if the day of the month is currently 31, and the month value is changed from January (0) to September (8), the date will become October the 1st, as September only has 30 days. Similarly, a seconds value of 60 or 61 (a leap second) may result in the seconds value being reset to 0 and the minutes value being incremented by 1, if the new time does not include a leap second.

Parameters: month the month, with a zero-based index from January.

See Also: getMonth Calendar

setSeconds

public void setSeconds(int seconds)

Deprecated: Use Calendar instead of Date, and use set(Calendar.SECOND, seconds) instead.

Sets the seconds to the given value. The other fields are only altered as necessary to match the same date and time in the new minute. In most cases, the other fields won't change at all. However, in the case of a leap second, values may be adjusted. For example, setting the seconds value to 60 or 61 (a leap second) may result in the seconds value being reset to 0 and the minutes value being incremented by 1, if the current time does not contain a leap second.

Parameters: seconds the seconds.

See Also: Calendar getSeconds

setTime

public void setTime(long time)
Sets the time which this object should represent.

Parameters: time the time in milliseconds since the epoch.

setYear

public void setYear(int year)

Deprecated: Use Calendar instead of Date, and use set(Calendar.YEAR, year) instead. Note about the 1900 difference in year.

Sets the year to the specified year, plus 1900. The other fields are only altered as required to match the same date and time in the new year. Usually, this will mean that the fields are not changed at all, but in the case of a leap day or leap second, the fields will change in relation to the existence of such an event in the new year. For example, if the date specifies February the 29th, 2000, then this will become March the 1st if the year is changed to 2001, as 2001 is not a leap year. Similarly, a seconds value of 60 or 61 may result in the seconds becoming 0 and the minute increasing by 1, if the new time does not include a leap second.

Parameters: year the year minus 1900.

See Also: getYear Calendar

toGMTString

public String toGMTString()

Deprecated: Use DateFormat.format(Date) with a GMT TimeZone.

Returns a string representation of this Date object using GMT rather than the local timezone. The following date format is used:

d mon yyyy hh:mm:ss GMT

where the fields used here are:

Returns: A string of the form 'd mon yyyy hh:mm:ss GMT' using GMT as opposed to the local timezone.

See Also: parse DateFormat

toLocaleString

public String toLocaleString()

Deprecated: Use DateFormat.format(Date)

Returns a locale-dependent string representation of this Date object.

Returns: A locale-dependent string representation.

See Also: parse DateFormat

toString

public String toString()

Returns a string representation of this date using the following date format:

day mon dd hh:mm:ss zz yyyy

where the fields used here are:

The DateFormat class should now be preferred over using this method.

Returns: A string of the form 'day mon dd hh:mm:ss zz yyyy'

See Also: parse DateFormat

UTC

public static long UTC(int year, int month, int date, int hrs, int min, int sec)

Deprecated: Use Calendar with a UTC TimeZone instead.

Returns the number of milliseconds since the epoch specified by the given arguments. The arguments are interpreted relative to UTC rather than the local time zone.

Parameters: year the difference between the required year and 1900. month the month as a value between 0 and 11. date the day as a value between 0 and 31. hrs the hour as a value between 0 and 23, in 24-hour clock notation. min the minute as a value between 0 and 59. sec the second as a value between 0 and 61 (with 60 and 61 being leap seconds).

Returns: the time in milliseconds since the epoch.