Package java.util

Class Date

java.lang.Object
java.util.Date
All Implemented Interfaces:
Serializable, Cloneable, Comparable<Date>
Direct Known Subclasses:
Date, Time, Timestamp

public class Date
extends Object
implements Serializable, Cloneable, Comparable<Date>
A specific moment in time, with millisecond precision. Values typically come from System.currentTimeMillis(), and are always UTC, regardless of the system's time zone. This is often called "Unix time" or "epoch time".

Instances of this class are suitable for comparison, but little else. Use DateFormat to format a Date for display to a human. Use Calendar to break down a Date if you need to extract fields such as the current month or day of week, or to construct a Date from a broken-down time. That is: this class' deprecated display-related functionality is now provided by DateFormat, and this class' deprecated computational functionality is now provided by Calendar. Both of these other classes (and their subclasses) allow you to interpret a Date in a given time zone.

Note that, surprisingly, instances of this class are mutable.

See Also:
Serialized Form
  • Constructor Summary

    Constructors
    Constructor Description
    Date()
    Initializes this Date instance to the current time.
    Date​(int year, int month, int day)
    Deprecated.
    Date​(int year, int month, int day, int hour, int minute)
    Date​(int year, int month, int day, int hour, int minute, int second)
    Date​(long milliseconds)
    Initializes this Date instance using the specified millisecond value.
    Date​(String string)
    Deprecated.
    Use DateFormat instead.
  • Method Summary

    Modifier and Type Method Description
    boolean after​(Date date)
    Returns if this Date is after the specified Date.
    boolean before​(Date date)
    Returns if this Date is before the specified Date.
    Object clone()
    Returns a new Date with the same millisecond value as this Date.
    int compareTo​(Date date)
    Compare the receiver to the specified Date to determine the relative ordering.
    boolean equals​(Object object)
    Compares the specified object to this Date and returns if they are equal.
    int getDate()
    Deprecated.
    Use Calendar.get(Calendar.DATE) instead.
    int getDay()
    Deprecated.
    Use Calendar.get(Calendar.DAY_OF_WEEK) instead.
    int getHours()
    Deprecated.
    Use Calendar.get(Calendar.HOUR_OF_DAY) instead.
    int getMinutes()
    Deprecated.
    Use Calendar.get(Calendar.MINUTE) instead.
    int getMonth()
    Deprecated.
    Use Calendar.get(Calendar.MONTH) instead.
    int getSeconds()
    Deprecated.
    Use Calendar.get(Calendar.SECOND) instead.
    long getTime()
    Returns this Date as a millisecond value.
    int getTimezoneOffset()
    Deprecated.
    Use (Calendar.get(Calendar.ZONE_OFFSET) + Calendar.get(Calendar.DST_OFFSET)) / 60000 instead.
    int getYear()
    Deprecated.
    Use Calendar.get(Calendar.YEAR) - 1900 instead.
    int hashCode()
    Returns an integer hash code for the receiver.
    static long parse​(String string)
    Deprecated.
    Use DateFormat instead.
    void setDate​(int day)
    Deprecated.
    Use Calendar.set(Calendar.DATE, day) instead.
    void setHours​(int hour)
    Deprecated.
    Use Calendar.set(Calendar.HOUR_OF_DAY, hour) instead.
    void setMinutes​(int minute)
    Deprecated.
    Use Calendar.set(Calendar.MINUTE, minute) instead.
    void setMonth​(int month)
    Deprecated.
    Use Calendar.set(Calendar.MONTH, month) instead.
    void setSeconds​(int second)
    Deprecated.
    Use Calendar.set(Calendar.SECOND, second) instead.
    void setTime​(long milliseconds)
    Sets this Date to the specified millisecond value.
    void setYear​(int year)
    Deprecated.
    Use Calendar.set(Calendar.YEAR, year + 1900) instead.
    String toGMTString()
    Deprecated.
    Use DateFormat instead.
    String toLocaleString()
    Deprecated.
    Use DateFormat instead.
    String toString()
    Returns a string representation of this Date.
    static long UTC​(int year, int month, int day, int hour, int minute, int second)
    Deprecated.
    Use code like this instead: Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT")); cal.set(year + 1900, month, day, hour, minute, second); cal.getTime().getTime();

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Date

      public Date()
      Initializes this Date instance to the current time.
    • Date

      @Deprecated public Date​(int year, int month, int day)
      Deprecated.
      Constructs a new Date initialized to midnight in the default TimeZone on the specified date.
      Parameters:
      year - the year, 0 is 1900.
      month - the month, 0 - 11.
      day - the day of the month, 1 - 31.
    • Date

      @Deprecated public Date​(int year, int month, int day, int hour, int minute)
      Constructs a new Date initialized to the specified date and time in the default TimeZone.
      Parameters:
      year - the year, 0 is 1900.
      month - the month, 0 - 11.
      day - the day of the month, 1 - 31.
      hour - the hour of day, 0 - 23.
      minute - the minute of the hour, 0 - 59.
    • Date

      @Deprecated public Date​(int year, int month, int day, int hour, int minute, int second)
      Constructs a new Date initialized to the specified date and time in the default TimeZone.
      Parameters:
      year - the year, 0 is 1900.
      month - the month, 0 - 11.
      day - the day of the month, 1 - 31.
      hour - the hour of day, 0 - 23.
      minute - the minute of the hour, 0 - 59.
      second - the second of the minute, 0 - 59.
    • Date

      public Date​(long milliseconds)
      Initializes this Date instance using the specified millisecond value. The value is the number of milliseconds since Jan. 1, 1970 GMT.
      Parameters:
      milliseconds - the number of milliseconds since Jan. 1, 1970 GMT.
    • Date

      @Deprecated public Date​(String string)
      Deprecated.
      Use DateFormat instead.
      Constructs a new Date initialized to the date and time parsed from the specified String.
      Parameters:
      string - the String to parse.
  • Method Details

    • after

      public boolean after​(Date date)
      Returns if this Date is after the specified Date.
      Parameters:
      date - a Date instance to compare.
      Returns:
      true if this Date is after the specified Date, false otherwise.
    • before

      public boolean before​(Date date)
      Returns if this Date is before the specified Date.
      Parameters:
      date - a Date instance to compare.
      Returns:
      true if this Date is before the specified Date, false otherwise.
    • clone

      public Object clone()
      Returns a new Date with the same millisecond value as this Date.
      Overrides:
      clone in class Object
      Returns:
      a shallow copy of this Date.
      See Also:
      Cloneable
    • compareTo

      public int compareTo​(Date date)
      Compare the receiver to the specified Date to determine the relative ordering.
      Specified by:
      compareTo in interface Comparable<Date>
      Parameters:
      date - a Date to compare against.
      Returns:
      an int < 0 if this Date is less than the specified Date, 0 if they are equal, and an int > 0 if this Date is greater.
    • equals

      public boolean equals​(Object object)
      Compares the specified object to this Date and returns if they are equal. To be equal, the object must be an instance of Date and have the same millisecond value.
      Overrides:
      equals in class Object
      Parameters:
      object - the object to compare with this object.
      Returns:
      true if the specified object is equal to this Date, false otherwise.
      See Also:
      hashCode()
    • getDate

      @Deprecated public int getDate()
      Deprecated.
      Use Calendar.get(Calendar.DATE) instead.
      Returns the gregorian calendar day of the month for this Date object.
      Returns:
      the day of the month.
    • getDay

      @Deprecated public int getDay()
      Deprecated.
      Use Calendar.get(Calendar.DAY_OF_WEEK) instead.
      Returns the gregorian calendar day of the week for this Date object.
      Returns:
      the day of the week.
    • getHours

      @Deprecated public int getHours()
      Deprecated.
      Use Calendar.get(Calendar.HOUR_OF_DAY) instead.
      Returns the gregorian calendar hour of the day for this Date object.
      Returns:
      the hour of the day.
    • getMinutes

      @Deprecated public int getMinutes()
      Deprecated.
      Use Calendar.get(Calendar.MINUTE) instead.
      Returns the gregorian calendar minute of the hour for this Date object.
      Returns:
      the minutes.
    • getMonth

      @Deprecated public int getMonth()
      Deprecated.
      Use Calendar.get(Calendar.MONTH) instead.
      Returns the gregorian calendar month for this Date object.
      Returns:
      the month.
    • getSeconds

      @Deprecated public int getSeconds()
      Deprecated.
      Use Calendar.get(Calendar.SECOND) instead.
      Returns the gregorian calendar second of the minute for this Date object.
      Returns:
      the seconds.
    • getTime

      public long getTime()
      Returns this Date as a millisecond value. The value is the number of milliseconds since Jan. 1, 1970, midnight GMT.
      Returns:
      the number of milliseconds since Jan. 1, 1970, midnight GMT.
    • getTimezoneOffset

      @Deprecated public int getTimezoneOffset()
      Deprecated.
      Use (Calendar.get(Calendar.ZONE_OFFSET) + Calendar.get(Calendar.DST_OFFSET)) / 60000 instead.
      Returns the timezone offset in minutes of the default TimeZone.
      Returns:
      the timezone offset in minutes of the default TimeZone.
    • getYear

      @Deprecated public int getYear()
      Deprecated.
      Use Calendar.get(Calendar.YEAR) - 1900 instead.
      Returns the gregorian calendar year since 1900 for this Date object.
      Returns:
      the year - 1900.
    • hashCode

      public int hashCode()
      Returns an integer hash code for the receiver. Objects which are equal return the same value for this method.
      Overrides:
      hashCode in class Object
      Returns:
      this Date's hash.
      See Also:
      equals(java.lang.Object)
    • parse

      @Deprecated public static long parse​(String string)
      Deprecated.
      Use DateFormat instead.
      Returns the millisecond value of the date and time parsed from the specified String. Many date/time formats are recognized, including IETF standard syntax, i.e. Tue, 22 Jun 1999 12:16:00 GMT-0500
      Parameters:
      string - the String to parse.
      Returns:
      the millisecond value parsed from the String.
    • setDate

      @Deprecated public void setDate​(int day)
      Deprecated.
      Use Calendar.set(Calendar.DATE, day) instead.
      Sets the gregorian calendar day of the month for this Date object.
      Parameters:
      day - the day of the month.
    • setHours

      @Deprecated public void setHours​(int hour)
      Deprecated.
      Use Calendar.set(Calendar.HOUR_OF_DAY, hour) instead.
      Sets the gregorian calendar hour of the day for this Date object.
      Parameters:
      hour - the hour of the day.
    • setMinutes

      @Deprecated public void setMinutes​(int minute)
      Deprecated.
      Use Calendar.set(Calendar.MINUTE, minute) instead.
      Sets the gregorian calendar minute of the hour for this Date object.
      Parameters:
      minute - the minutes.
    • setMonth

      @Deprecated public void setMonth​(int month)
      Deprecated.
      Use Calendar.set(Calendar.MONTH, month) instead.
      Sets the gregorian calendar month for this Date object.
      Parameters:
      month - the month.
    • setSeconds

      @Deprecated public void setSeconds​(int second)
      Deprecated.
      Use Calendar.set(Calendar.SECOND, second) instead.
      Sets the gregorian calendar second of the minute for this Date object.
      Parameters:
      second - the seconds.
    • setTime

      public void setTime​(long milliseconds)
      Sets this Date to the specified millisecond value. The value is the number of milliseconds since Jan. 1, 1970 GMT.
      Parameters:
      milliseconds - the number of milliseconds since Jan. 1, 1970 GMT.
    • setYear

      @Deprecated public void setYear​(int year)
      Deprecated.
      Use Calendar.set(Calendar.YEAR, year + 1900) instead.
      Sets the gregorian calendar year since 1900 for this Date object.
      Parameters:
      year - the year since 1900.
    • toGMTString

      @Deprecated public String toGMTString()
      Deprecated.
      Use DateFormat instead.
      Returns the string representation of this Date in GMT in the format "22 Jun 1999 13:02:00 GMT".
    • toLocaleString

      @Deprecated public String toLocaleString()
      Deprecated.
      Use DateFormat instead.
      Returns the string representation of this Date for the default Locale.
    • toString

      public String toString()
      Returns a string representation of this Date. The formatting is equivalent to using a SimpleDateFormat with the format string "EEE MMM dd HH:mm:ss zzz yyyy", which looks something like "Tue Jun 22 13:07:00 PDT 1999". The current default time zone and locale are used. If you need control over the time zone or locale, use SimpleDateFormat instead.
      Overrides:
      toString in class Object
      Returns:
      a printable representation of this object.
    • UTC

      @Deprecated public static long UTC​(int year, int month, int day, int hour, int minute, int second)
      Deprecated.
      Use code like this instead: Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT")); cal.set(year + 1900, month, day, hour, minute, second); cal.getTime().getTime();
      Returns the millisecond value of the specified date and time in GMT.
      Parameters:
      year - the year, 0 is 1900.
      month - the month, 0 - 11.
      day - the day of the month, 1 - 31.
      hour - the hour of day, 0 - 23.
      minute - the minute of the hour, 0 - 59.
      second - the second of the minute, 0 - 59.
      Returns:
      the date and time in GMT in milliseconds.