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
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 thisDateinstance to the current time.Date(int year, int month, int day)Deprecated.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
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 Details
-
Date
public Date()Initializes thisDateinstance to the current time. -
Date
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.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.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.UseDateFormatinstead.Constructs a newDateinitialized to the date and time parsed from the specified String.- Parameters:
string- the String to parse.
-
-
Method Details
-
after
Returns if thisDateis after the specified Date.- Parameters:
date- a Date instance to compare.- Returns:
trueif thisDateis after the specifiedDate,falseotherwise.
-
before
Returns if thisDateis before the specified Date.- Parameters:
date- aDateinstance to compare.- Returns:
trueif thisDateis before the specifiedDate,falseotherwise.
-
clone
Returns a newDatewith the same millisecond value as thisDate. -
compareTo
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
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.UseCalendar.get(Calendar.DATE)instead.Returns the gregorian calendar day of the month for thisDateobject.- Returns:
- the day of the month.
-
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.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.UseCalendar.get(Calendar.MINUTE)instead.Returns the gregorian calendar minute of the hour for thisDateobject.- Returns:
- the minutes.
-
getMonth
Deprecated.UseCalendar.get(Calendar.MONTH)instead.Returns the gregorian calendar month for thisDateobject.- Returns:
- the month.
-
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.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.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.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.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.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.UseCalendar.set(Calendar.MINUTE, minute)instead.Sets the gregorian calendar minute of the hour for thisDateobject.- Parameters:
minute- the minutes.
-
setMonth
Deprecated.UseCalendar.set(Calendar.MONTH, month)instead.Sets the gregorian calendar month for thisDateobject.- Parameters:
month- the month.
-
setSeconds
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.UseCalendar.set(Calendar.YEAR, year + 1900)instead.Sets the gregorian calendar year since 1900 for thisDateobject.- Parameters:
year- the year since 1900.
-
toGMTString
Deprecated.UseDateFormatinstead.Returns the string representation of thisDatein GMT in the format"22 Jun 1999 13:02:00 GMT". -
toLocaleString
Deprecated.UseDateFormatinstead.Returns the string representation of thisDatefor the defaultLocale. -
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". The current default time zone and locale are used. If you need control over the time zone or locale, useSimpleDateFormatinstead. -
UTC
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.
-
GregorianCalendar(int, int, int)instead.