public class Date extends Object implements Serializable, Cloneable, Comparable<Date>
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.
| Constructor and Description |
|---|
Date()
Initializes this
Date instance to the current time. |
Date(int year,
int month,
int day)
Deprecated.
Use
GregorianCalendar(int, int, int) instead. |
Date(int year,
int month,
int day,
int hour,
int minute)
Deprecated.
Use
GregorianCalendar(int, int, int, int, int) instead. |
Date(int year,
int month,
int day,
int hour,
int minute,
int second)
Deprecated.
Use
GregorianCalendar(int, int, int, int, int, int)
instead. |
Date(long milliseconds)
Initializes this
Date instance using the specified millisecond value. |
Date(String string)
Deprecated.
Use
DateFormat instead. |
| Modifier and Type | Method and 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. |
static Date |
from(Instant instant)
Obtains an instance of
Date from an Instant object. |
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. |
Instant |
toInstant()
Converts this
Date object to an Instant. |
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(); |
public Date()
Date instance to the current time.@Deprecated public Date(int year, int month, int day)
GregorianCalendar(int, int, int) instead.Date initialized to midnight in the default TimeZone on
the specified date.year - the year, 0 is 1900.month - the month, 0 - 11.day - the day of the month, 1 - 31.@Deprecated public Date(int year, int month, int day, int hour, int minute)
GregorianCalendar(int, int, int, int, int) instead.Date initialized to the specified date and time in the
default TimeZone.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.@Deprecated public Date(int year, int month, int day, int hour, int minute, int second)
GregorianCalendar(int, int, int, int, int, int)
instead.Date initialized to the specified date and time in the
default TimeZone.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.public Date(long milliseconds)
Date instance using the specified millisecond value. The
value is the number of milliseconds since Jan. 1, 1970 GMT.milliseconds - the number of milliseconds since Jan. 1, 1970 GMT.@Deprecated public Date(String string)
DateFormat instead.Date initialized to the date and time parsed from the
specified String.string - the String to parse.public boolean after(Date date)
Date is after the specified Date.date - a Date instance to compare.true if this Date is after the specified Date,
false otherwise.public boolean before(Date date)
Date is before the specified Date.date - a Date instance to compare.true if this Date is before the specified Date,
false otherwise.public Object clone()
Date with the same millisecond value as this Date.public int compareTo(Date date)
Date to determine the relative
ordering.compareTo in interface Comparable<Date>date - a Date to compare against.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.public boolean equals(Object object)
Date and returns if they are equal.
To be equal, the object must be an instance of Date and have the same millisecond
value.equals in class Objectobject - the object to compare with this object.true if the specified object is equal to this Date, false
otherwise.hashCode()@Deprecated public int getDate()
Calendar.get(Calendar.DATE) instead.Date object.@Deprecated public int getDay()
Calendar.get(Calendar.DAY_OF_WEEK) instead.Date object.@Deprecated public int getHours()
Calendar.get(Calendar.HOUR_OF_DAY) instead.Date object.@Deprecated public int getMinutes()
Calendar.get(Calendar.MINUTE) instead.Date object.@Deprecated public int getMonth()
Calendar.get(Calendar.MONTH) instead.Date object.@Deprecated public int getSeconds()
Calendar.get(Calendar.SECOND) instead.Date object.public long getTime()
Date as a millisecond value. The value is the number of
milliseconds since Jan. 1, 1970, midnight GMT.@Deprecated public int getTimezoneOffset()
(Calendar.get(Calendar.ZONE_OFFSET) + Calendar.get(Calendar.DST_OFFSET)) / 60000 instead.TimeZone.TimeZone.@Deprecated public int getYear()
Calendar.get(Calendar.YEAR) - 1900 instead.Date object.public int hashCode()
hashCode in class ObjectDate's hash.equals(java.lang.Object)@Deprecated public static long parse(String string)
DateFormat instead.String. Many date/time formats are recognized, including IETF
standard syntax, i.e. Tue, 22 Jun 1999 12:16:00 GMT-0500string - the String to parse.@Deprecated public void setDate(int day)
Calendar.set(Calendar.DATE, day) instead.Date object.day - the day of the month.@Deprecated public void setHours(int hour)
Calendar.set(Calendar.HOUR_OF_DAY, hour) instead.Date object.hour - the hour of the day.@Deprecated public void setMinutes(int minute)
Calendar.set(Calendar.MINUTE, minute) instead.Date object.minute - the minutes.@Deprecated public void setMonth(int month)
Calendar.set(Calendar.MONTH, month) instead.Date object.month - the month.@Deprecated public void setSeconds(int second)
Calendar.set(Calendar.SECOND, second) instead.Date object.second - the seconds.public void setTime(long milliseconds)
Date to the specified millisecond value. The value is the
number of milliseconds since Jan. 1, 1970 GMT.milliseconds - the number of milliseconds since Jan. 1, 1970 GMT.@Deprecated public void setYear(int year)
Calendar.set(Calendar.YEAR, year + 1900) instead.Date object.year - the year since 1900.@Deprecated public String toGMTString()
DateFormat instead.Date in GMT in the format
"22 Jun 1999 13:02:00 GMT".@Deprecated public String toLocaleString()
DateFormat instead.Date for the default Locale.public String toString()
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". While the current default time
zone is used, all formatting and timezone names follow Locale.US. If you need control
over the time zone or locale, use SimpleDateFormat instead.@Deprecated public static long UTC(int year, int month, int day, int hour, int minute, int second)
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
cal.set(year + 1900, month, day, hour, minute, second);
cal.getTime().getTime();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.public static Date from(Instant instant)
Date from an Instant object.
Instant uses a precision of nanoseconds, whereas Date
uses a precision of milliseconds. The conversion will trancate any
excess precision information as though the amount in nanoseconds was
subject to integer division by one million.
Instant can store points on the time-line further in the future
and further in the past than Date. In this scenario, this method
will throw an exception.
instant - the instant to convertDate representing the same point on the time-line as
the provided instantNullPointerException - if instant is null.IllegalArgumentException - if the instant is too large to
represent as a Datepublic Instant toInstant()
Date object to an Instant.
The conversion creates an Instant that represents the same
point on the time-line as this Date.
Date object