java.util
public class Date extends Object implements Cloneable, Comparable<Date>, 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.
Calendar
,
GregorianCalendar
,
DateFormat
,
Serialized FormConstructor and Description |
---|
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(String s)
Deprecated.
use
java.text.DateFormat.parse(s) instead. |
Modifier and Type | Method and Description |
---|---|
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.
|
Object |
clone()
Returns a copy of this
Date object. |
int |
compareTo(Date when)
Compares two dates.
|
boolean |
equals(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(String string)
Deprecated.
Use DateFormat.parse(String)
|
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.
|
String |
toGMTString()
Deprecated.
Use DateFormat.format(Date) with a GMT TimeZone.
|
String |
toLocaleString()
Deprecated.
Use DateFormat.format(Date)
|
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). |
static long |
UTC(int year,
int month,
int date,
int hrs,
int min,
int sec)
Deprecated.
Use
Calendar with a UTC
TimeZone instead. |
public Date()
public Date(long time)
time
- the time in milliseconds since the epoch.public Date(int year, int month, int day)
new GregorianCalendar(year+1900, month,
day)
instead.year
- the difference between the required year and 1900.month
- the month as a value between 0 and 11.day
- the day as a value between 0 and 31.public Date(int year, int month, int day, int hour, int min)
new GregorianCalendar(year+1900, month,
day, hour, min)
instead.year
- the difference between the required year and 1900.month
- the month as a value between 0 and 11.day
- the day as a value between 0 and 31.hour
- the hour as a value between 0 and 23, in 24-hour
clock notation.min
- the minute as a value between 0 and 59.public Date(int year, int month, int day, int hour, int min, int sec)
new GregorianCalendar(year+1900, month,
day, hour, min, sec)
instead.year
- the difference between the required year and 1900.month
- the month as a value between 0 and 11.day
- the day as a value between 0 and 31.hour
- the hour as a value between 0 and 23, in 24-hour
clock notation.min
- the minute as a value between 0 and 59.sec
- the second as a value between 0 and 61 (with 60
and 61 being leap seconds).public Date(String s)
java.text.DateFormat.parse(s)
instead.new Date(Date.parse(s))
parse(java.lang.String)
public Object clone()
Date
object.clone
in class Object
Object.clone()
public static long UTC(int year, int month, int date, int hrs, int min, int sec)
Calendar
with a UTC
TimeZone
instead.year
- the difference between the required year and 1900.month
- the month as a value between 0 and 11.date
- the day as a value between 0 and 31.hrs
- the hour as a value between 0 and 23, in 24-hour
clock notation.min
- the minute as a value between 0 and 59.sec
- the second as a value between 0 and 61 (with 60
and 61 being leap seconds).public long getTime()
public int getTimezoneOffset()
Calendar.get(Calendar.ZONE_OFFSET)+Calendar.get(Calendar.DST_OFFSET)
instead.public void setTime(long time)
time
- the time in milliseconds since the epoch.public boolean after(Date when)
when
- the other datepublic boolean before(Date when)
when
- the other datepublic boolean equals(Object obj)
equals
in class Object
obj
- the object to compare.Object.hashCode()
public int compareTo(Date when)
compareTo
in interface Comparable<Date>
when
- the other date.public int hashCode()
Date
as the
XOR of the most significant and the least significant
32 bits of the 64 bit milliseconds value.hashCode
in class Object
Object.equals(Object)
,
System.identityHashCode(Object)
public 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.
toString
in class Object
parse(String)
,
DateFormat
public String toLocaleString()
Date
object.parse(String)
,
DateFormat
public String toGMTString()
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.
parse(String)
,
DateFormat
public static long parse(String 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:
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).
A sequence of consecutive alphabetic characters is recognised as a word, and interpreted as follows, in a case-insentive fashion:
string
- The String to parse.IllegalArgumentException
- if the string fails to parse.toString()
,
SimpleDateFormat
public int getYear()
Date
object and 1900.Calendar
,
setYear(int)
public void setYear(int year)
public int getMonth()
Date
object,
as a value between 0 (January) and 11 (December).setMonth(int)
,
Calendar
public void setMonth(int month)
month
- the month, with a zero-based index
from January.getMonth()
,
Calendar
public int getDate()
Date
object, as a value between 0 and 31.Calendar
,
setDate(int)
public void setDate(int date)
public int getDay()
Date
object as an integer between 0 (Sunday) and 6 (Saturday).Calendar
public int getHours()
Date
object as an integer between 0 and 23.Calendar
,
setHours(int)
public void setHours(int hours)
hours
- the hours.Calendar
,
getHours()
public int getMinutes()
Date
object, as an integer between 0 and 59.Calendar
,
setMinutes(int)
public void setMinutes(int minutes)
minutes
- the minutes.Calendar
,
getMinutes()
public int getSeconds()
Date
object, as an integer between 0 and 61 (60 and 61 being leap seconds).Calendar
,
setSeconds(int)
public void setSeconds(int seconds)
seconds
- the seconds.Calendar
,
getSeconds()