Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

java.util
Class Date  view Date download Date.java

java.lang.Object
  extended byjava.util.Date
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Comparable, java.io.Serializable

public class Date
extends java.lang.Object
implements java.lang.Cloneable, java.lang.Comparable, java.io.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.


Field Summary
private static java.lang.String[] monthNames
          An array of month names used to map names to integer values.
private static long serialVersionUID
          This is the serialization UID for this class for compatability with Sun's JDK.
private  long time
          The time in milliseconds since the epoch.
private static java.lang.String[] weekNames
          An array of week names used to map names to integer values.
 
Constructor Summary
Date()
          Creates a new Date Object representing the current time.
Date(int year, int month, int day)
          Deprecated. use new GregorianCalendar(year+1900, month, day) instead.
Date(int year, int month, int day, int hour, int min)
          Deprecated. use new GregorianCalendar(year+1900, month, day, hour, min) instead.
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.
Date(long time)
          Creates a new Date Object representing the given time.
Date(java.lang.String s)
          Deprecated. use java.text.DateFormat.parse(s) instead.
 
Method Summary
 boolean after(Date when)
          Tests if this date is after the specified date.
 boolean before(Date when)
          Tests if this date is before the specified date.
 java.lang.Object clone()
          Returns a copy of this Date object.
 int compareTo(Date when)
          Compares two dates.
 int compareTo(java.lang.Object obj)
          Compares this Date to another object.
 boolean equals(java.lang.Object obj)
          Compares two dates for equality.
 int getDate()
          Deprecated. Use Calendar instead of Date, and use get(Calendar.DATE) instead.
 int getDay()
          Deprecated. Use Calendar instead of Date, and use get(Calendar.DAY_OF_WEEK) instead.
 int getHours()
          Deprecated. Use Calendar instead of Date, and use get(Calendar.HOUR_OF_DAY) instead.
 int getMinutes()
          Deprecated. Use Calendar instead of Date, and use get(Calendar.MINUTE) instead.
 int getMonth()
          Deprecated. Use Calendar instead of Date, and use get(Calendar.MONTH) instead.
 int getSeconds()
          Deprecated. Use Calendar instead of Date, and use get(Calendar.SECOND) instead.
 long getTime()
          Gets the time represented by this object.
 int getTimezoneOffset()
          Deprecated. use Calendar.get(Calendar.ZONE_OFFSET)+Calendar.get(Calendar.DST_OFFSET) instead.
 int getYear()
          Deprecated. Use Calendar instead of Date, and use get(Calendar.YEAR) instead. Note the 1900 difference in the year.
 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.
static long parse(java.lang.String string)
          Deprecated. Use DateFormat.parse(String)
private static boolean parseDayOfWeek(java.lang.String tok)
          Parses the day of the week string.
private static int parseMonth(java.lang.String tok)
          Parses the month string.
private static int parseTz(java.lang.String tok, char sign)
          Parses the time zone string.
private  void readObject(java.io.ObjectInputStream input)
          Deserializes a Date object from an input stream, setting the time (in milliseconds since the epoch) to the long value read from the stream.
 void setDate(int date)
          Deprecated. Use Calendar instead of Date, and use set(Calendar.DATE, date) instead.
 void setHours(int hours)
          Deprecated. Use Calendar instead of Date, and use set(Calendar.HOUR_OF_DAY, hours) instead.
 void setMinutes(int minutes)
          Deprecated. Use Calendar instead of Date, and use set(Calendar.MINUTE, minutes) instead.
 void setMonth(int month)
          Deprecated. Use Calendar instead of Date, and use set(Calendar.MONTH, month) instead.
 void setSeconds(int seconds)
          Deprecated. Use Calendar instead of Date, and use set(Calendar.SECOND, seconds) instead.
 void setTime(long time)
          Sets the time which this object should represent.
 void setYear(int year)
          Deprecated. Use Calendar instead of Date, and use set(Calendar.YEAR, year) instead. Note about the 1900 difference in year.
 java.lang.String toGMTString()
          Deprecated. Use DateFormat.format(Date) with a GMT TimeZone.
 java.lang.String toLocaleString()
          Deprecated. Use DateFormat.format(Date)
 java.lang.String toString()
           Returns a string representation of this date using the following date format:
static long UTC(int year, int month, int date, int hrs, int min, int sec)
          Deprecated. Use Calendar with a UTC TimeZone instead.
private  void writeObject(java.io.ObjectOutputStream output)
          Serializes a Date object to an output stream, storing the time (in milliseconds since the epoch) as a long value in the stream.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
This is the serialization UID for this class for compatability with Sun's JDK.

See Also:
Constant Field Values

time

private transient long time
The time in milliseconds since the epoch.


weekNames

private static final java.lang.String[] weekNames
An array of week names used to map names to integer values.


monthNames

private static final java.lang.String[] monthNames
An array of month names used to map names to integer values.

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.


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.


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.


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.


Date

public Date(java.lang.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))

Method Detail

clone

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


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.


getTime

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


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.


setTime

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


after

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


before

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


equals

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


compareTo

public int compareTo(Date when)
Compares two dates.


compareTo

public int compareTo(java.lang.Object obj)
Compares this Date to another object. This behaves like compareTo(Date), but it takes a generic object and throws a ClassCastException if obj is not a Date.

Specified by:
compareTo in interface java.lang.Comparable

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.


toString

public java.lang.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:

  • day -- the day of the week (Sunday through to Saturday).
  • mon -- the month (Jan to Dec).
  • dd -- the day of the month as two decimal digits (01 to 31).
  • hh -- the hour of the day as two decimal digits in 24-hour clock notation (01 to 23).
  • mm -- the minute of the day as two decimal digits (01 to 59).
  • ss -- the second of the day as two decimal digits (01 to 61).
  • zz -- the time zone information if available. The possible time zones used include the abbreviations recognised by parse() (e.g. GMT, CET, etc.) and may reflect the fact that daylight savings time is in effect. The empty string is used if there is no time zone information.
  • yyyy -- the year as four decimal digits.

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


toLocaleString

public java.lang.String toLocaleString()
Deprecated. Use DateFormat.format(Date)

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


toGMTString

public java.lang.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:

  • d -- the day of the month as one or two decimal digits (1 to 31).
  • mon -- the month (Jan to Dec).
  • yyyy -- the year as four decimal digits.
  • hh -- the hour of the day as two decimal digits in 24-hour clock notation (01 to 23).
  • mm -- the minute of the day as two decimal digits (01 to 59).
  • ss -- the second of the day as two decimal digits (01 to 61).
  • GMT -- the literal string "GMT" indicating Greenwich Mean Time as opposed to the local timezone.


parseTz

private static int parseTz(java.lang.String tok,
                           char sign)
                    throws java.lang.IllegalArgumentException
Parses the time zone string.


parseMonth

private static int parseMonth(java.lang.String tok)
Parses the month string.


parseDayOfWeek

private static boolean parseDayOfWeek(java.lang.String tok)
Parses the day of the week string.


parse

public static long parse(java.lang.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 number preceded by a sign (+ or -) is taken to be a time zone offset. The time zone offset can be specified in either hours or minutes. The former is assumed if the number is less than 24. Otherwise, the offset is assumed to be in minutes. A - indicates a time zone west of GMT, while a + represents a time zone to the east of GMT. The time zones are always assumed to be relative to GMT, and a (redundant) specification of this can be included with the time zone. For example, '-9', 'utc-9' and 'GMT-9' all represent a time zone nine hours west of GMT. Similarly, '+4', 'ut+4' and 'UTC+4' all give 4 hours east of GMT.
  • A number equal to or greater than 70 is regarded as a year specification. Values lower than 70 are only assumed to indicate a year if both the day of the month and the month itself have already been recognised. Year values less than 100 are interpreted as being relative to the current century when the Date class is initialised.. Given a century, x, the year is assumed to be within the range x - 80 to x + 19. The value itself is then used as a match against the two last digits of one of these years. For example, take x to be 2004. A two-digit year is assumed to fall within the range x - 80 (1924) and x + 19 (2023). Thus, any intepreted value between 0 and 23 is assumed to be 2000 to 2023 and values between 24 and 99 are taken as being 1924 to 1999. This only applies for the case of 2004. With a different year, the values will be interpreted differently. 2005 will used 0 to 24 as 2000 to 2024 and 25 to 99 as 1925 to 1999, for example. This behaviour differs from that of SimpleDateFormat and is time-dependent (a two-digit year will be interpreted differently depending on the time the code is run).
  • Numbers followed by a colon are interpreted by first an hour, and then as a minute, once an hour has been found.
  • Numbers followed by a slash are regarded first as a month, and then as a day of the month once the month has been found. This follows the U.S. date format of mm/dd, rather than the European dd/mm. Months are converted to the recognised value - 1 before storage, in order to put the number within the range 0 to 11.
  • Numbers followed by commas, whitespace, hyphens or the end of the string are interpreted in the following order: hour, minute, second, day of month. The first type not already recognised in the current string being parsed is assumed.

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

  • The characters 'AM' or 'PM' restrict the hour value to a value between 0 and 12. In the latter case, 12 is added to the hour value before storage.
  • Any words which match any prefix of one of the days of the week ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' and 'Sunday'), are simply ignored.
  • Any words which match any prefix of one of the months of the year ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December') are recognised and interpreted as the appropriate value between 0 and 11. The first match made against a month is the one used, in the order specified here. For example, 'Ma' is intepreted as 'March' (2) and not as 'May' (4). Similarly, 'Ju' is 'June', and not 'July'.
  • The words 'GMT', 'UT' and 'UTC' are interpreted as specifying UTC as the time zone in use for this date.
  • The word pairs 'EST'/'EDT', 'CST'/'CDT', 'MST'/'MDT' and 'PST'/'PDT' are interpreted as the appropriate U.S. time zone abbreviation. Each pair is the standard and daylight savings time zone specification, respectively, for each zone within the U.S, these being Eastern Standard/Daylight Time (-5), Central Standard/Daylight Time (-6), Mountain Standard/Daylight Time (-7) and Pacific Standard/Daylight Time (-8).


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.


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.


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).


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.


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.


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.


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).


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.


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.


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.


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.


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).


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.


readObject

private void readObject(java.io.ObjectInputStream input)
                 throws java.io.IOException,
                        java.lang.ClassNotFoundException
Deserializes a Date object from an input stream, setting the time (in milliseconds since the epoch) to the long value read from the stream.


writeObject

private void writeObject(java.io.ObjectOutputStream output)
                  throws java.io.IOException
Serializes a Date object to an output stream, storing the time (in milliseconds since the epoch) as a long value in the stream.