Package java.util
Class Date
- java.lang.Object
-
- java.util.Date
-
- All Implemented Interfaces:
Serializable,Cloneable,Comparable<Date>
public class Date extends Object implements Serializable, Cloneable, Comparable<Date>
A specific moment in time, with millisecond precision. Values typically come fromSystem.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
DateFormatto format aDatefor display to a human. UseCalendarto break down aDateif you need to extract fields such as the current month or day of week, or to construct aDatefrom a broken-down time. That is: this class' deprecated display-related functionality is now provided byDateFormat, and this class' deprecated computational functionality is now provided byCalendar. Both of these other classes (and their subclasses) allow you to interpret aDatein a given time zone.Note that, surprisingly, instances of this class are mutable.
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description Date()Initializes thisDateinstance to the current time.Date(int year, int month, int day)Deprecated.UseGregorianCalendar(int, int, int)instead.Date(int year, int month, int day, int hour, int minute)Deprecated.UseGregorianCalendar(int, int, int, int, int)instead.Date(int year, int month, int day, int hour, int minute, int second)Deprecated.UseGregorianCalendar(int, int, int, int, int, int)instead.Date(long milliseconds)Initializes thisDateinstance using the specified millisecond value.Date(String string)Deprecated.UseDateFormatinstead.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description booleanafter(Date date)Returns if thisDateis after the specified Date.booleanbefore(Date date)Returns if thisDateis before the specified Date.Objectclone()Returns a newDatewith the same millisecond value as thisDate.intcompareTo(Date date)Compare the receiver to the specifiedDateto determine the relative ordering.booleanequals(Object object)Compares the specified object to thisDateand returns if they are equal.intgetDate()Deprecated.UseCalendar.get(Calendar.DATE)instead.intgetDay()Deprecated.UseCalendar.get(Calendar.DAY_OF_WEEK)instead.intgetHours()Deprecated.UseCalendar.get(Calendar.HOUR_OF_DAY)instead.intgetMinutes()Deprecated.UseCalendar.get(Calendar.MINUTE)instead.intgetMonth()Deprecated.UseCalendar.get(Calendar.MONTH)instead.intgetSeconds()Deprecated.UseCalendar.get(Calendar.SECOND)instead.longgetTime()Returns thisDateas a millisecond value.intgetTimezoneOffset()Deprecated.Use(Calendar.get(Calendar.ZONE_OFFSET) + Calendar.get(Calendar.DST_OFFSET)) / 60000instead.intgetYear()Deprecated.UseCalendar.get(Calendar.YEAR) - 1900instead.inthashCode()Returns an integer hash code for the receiver.static longparse(String string)Deprecated.UseDateFormatinstead.voidsetDate(int day)Deprecated.UseCalendar.set(Calendar.DATE, day)instead.voidsetHours(int hour)Deprecated.UseCalendar.set(Calendar.HOUR_OF_DAY, hour)instead.voidsetMinutes(int minute)Deprecated.UseCalendar.set(Calendar.MINUTE, minute)instead.voidsetMonth(int month)Deprecated.UseCalendar.set(Calendar.MONTH, month)instead.voidsetSeconds(int second)Deprecated.UseCalendar.set(Calendar.SECOND, second)instead.voidsetTime(long milliseconds)Sets thisDateto the specified millisecond value.voidsetYear(int year)Deprecated.UseCalendar.set(Calendar.YEAR, year + 1900)instead.StringtoGMTString()Deprecated.UseDateFormatinstead.StringtoLocaleString()Deprecated.UseDateFormatinstead.StringtoString()Returns a string representation of thisDate.static longUTC(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();
-
-
-
Constructor Detail
-
Date
public Date()
Initializes thisDateinstance to the current time.
-
Date
@Deprecated public Date(int year, int month, int day)
Deprecated.UseGregorianCalendar(int, int, int)instead.Constructs a newDateinitialized to midnight in the defaultTimeZoneon 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)
Deprecated.UseGregorianCalendar(int, int, int, int, int)instead.Constructs a newDateinitialized to the specified date and time in the defaultTimeZone.- 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)
Deprecated.UseGregorianCalendar(int, int, int, int, int, int)instead.Constructs a newDateinitialized to the specified date and time in the defaultTimeZone.- 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 thisDateinstance 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.UseDateFormatinstead.Constructs a newDateinitialized to the date and time parsed from the specified String.- Parameters:
string- the String to parse.
-
-
Method Detail
-
after
public boolean after(Date date)
Returns if thisDateis after the specified Date.- Parameters:
date- a Date instance to compare.- Returns:
trueif thisDateis after the specifiedDate,falseotherwise.
-
before
public boolean before(Date date)
Returns if thisDateis before the specified Date.- Parameters:
date- aDateinstance to compare.- Returns:
trueif thisDateis before the specifiedDate,falseotherwise.
-
clone
public Object clone()
Returns a newDatewith the same millisecond value as thisDate.
-
compareTo
public int compareTo(Date date)
Compare the receiver to the specifiedDateto determine the relative ordering.- Specified by:
compareToin interfaceComparable<Date>- Parameters:
date- aDateto compare against.- Returns:
- an
int < 0if thisDateis less than the specifiedDate,0if they are equal, and anint > 0if thisDateis greater.
-
equals
public boolean equals(Object object)
Compares the specified object to thisDateand returns if they are equal. To be equal, the object must be an instance ofDateand have the same millisecond value.- Overrides:
equalsin classObject- Parameters:
object- the object to compare with this object.- Returns:
trueif the specified object is equal to thisDate,falseotherwise.- See Also:
hashCode()
-
getDate
@Deprecated public int getDate()
Deprecated.UseCalendar.get(Calendar.DATE)instead.Returns the gregorian calendar day of the month for thisDateobject.- Returns:
- the day of the month.
-
getDay
@Deprecated public int getDay()
Deprecated.UseCalendar.get(Calendar.DAY_OF_WEEK)instead.Returns the gregorian calendar day of the week for thisDateobject.- Returns:
- the day of the week.
-
getHours
@Deprecated public int getHours()
Deprecated.UseCalendar.get(Calendar.HOUR_OF_DAY)instead.Returns the gregorian calendar hour of the day for thisDateobject.- Returns:
- the hour of the day.
-
getMinutes
@Deprecated public int getMinutes()
Deprecated.UseCalendar.get(Calendar.MINUTE)instead.Returns the gregorian calendar minute of the hour for thisDateobject.- Returns:
- the minutes.
-
getMonth
@Deprecated public int getMonth()
Deprecated.UseCalendar.get(Calendar.MONTH)instead.Returns the gregorian calendar month for thisDateobject.- Returns:
- the month.
-
getSeconds
@Deprecated public int getSeconds()
Deprecated.UseCalendar.get(Calendar.SECOND)instead.Returns the gregorian calendar second of the minute for thisDateobject.- Returns:
- the seconds.
-
getTime
public long getTime()
Returns thisDateas 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)) / 60000instead.Returns the timezone offset in minutes of the defaultTimeZone.- Returns:
- the timezone offset in minutes of the default
TimeZone.
-
getYear
@Deprecated public int getYear()
Deprecated.UseCalendar.get(Calendar.YEAR) - 1900instead.Returns the gregorian calendar year since 1900 for thisDateobject.- 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:
hashCodein classObject- Returns:
- this
Date's hash. - See Also:
equals(java.lang.Object)
-
parse
@Deprecated public static long parse(String string)
Deprecated.UseDateFormatinstead.Returns the millisecond value of the date and time parsed from the specifiedString. 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.UseCalendar.set(Calendar.DATE, day)instead.Sets the gregorian calendar day of the month for thisDateobject.- Parameters:
day- the day of the month.
-
setHours
@Deprecated public void setHours(int hour)
Deprecated.UseCalendar.set(Calendar.HOUR_OF_DAY, hour)instead.Sets the gregorian calendar hour of the day for thisDateobject.- Parameters:
hour- the hour of the day.
-
setMinutes
@Deprecated public void setMinutes(int minute)
Deprecated.UseCalendar.set(Calendar.MINUTE, minute)instead.Sets the gregorian calendar minute of the hour for thisDateobject.- Parameters:
minute- the minutes.
-
setMonth
@Deprecated public void setMonth(int month)
Deprecated.UseCalendar.set(Calendar.MONTH, month)instead.Sets the gregorian calendar month for thisDateobject.- Parameters:
month- the month.
-
setSeconds
@Deprecated public void setSeconds(int second)
Deprecated.UseCalendar.set(Calendar.SECOND, second)instead.Sets the gregorian calendar second of the minute for thisDateobject.- Parameters:
second- the seconds.
-
setTime
public void setTime(long milliseconds)
Sets thisDateto 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.UseCalendar.set(Calendar.YEAR, year + 1900)instead.Sets the gregorian calendar year since 1900 for thisDateobject.- Parameters:
year- the year since 1900.
-
toGMTString
@Deprecated public String toGMTString()
Deprecated.UseDateFormatinstead.Returns the string representation of thisDatein GMT in the format"22 Jun 1999 13:02:00 GMT".
-
toLocaleString
@Deprecated public String toLocaleString()
Deprecated.UseDateFormatinstead.Returns the string representation of thisDatefor the defaultLocale.
-
toString
public String toString()
Returns a string representation of thisDate. The formatting is equivalent to using aSimpleDateFormatwith the format string "EEE MMM dd HH:mm:ss zzz yyyy", which looks something like "Tue Jun 22 13:07:00 PDT 1999". While the current default time zone is used, all formatting and timezone names followLocale.US. If you need control over the time zone or locale, useSimpleDateFormatinstead.
-
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.
-
-