508 Part II: The Java Library
As you can see by examining Table 18-3, theDatefeatures do not allow you to obtain
the individual components of the date or time. As the following program demonstrates, you
can only obtain the date and time in terms of milliseconds or in its default string representation
as returned bytoString( ). To obtain more-detailed information about the date and time,
you will use theCalendarclass.
// Show date and time using only Date methods.
import java.util.Date;
class DateDemo {
public static void main(String args[]) {
// Instantiate a Date object
Date date = new Date();
// display time and date using toString()
System.out.println(date);
// Display number of milliseconds since midnight, January 1, 1970 GMT
long msec = date.getTime();
System.out.println("Milliseconds since Jan. 1, 1970 GMT = " + msec);
}
}
Method Description
boolean after(Datedate) Returnstrueif the invokingDateobject contains a date that is
later than the one specified bydate.Other wise, it returnsfalse.
boolean before(Datedate) Returnstrueif the invokingDateobject contains a date that is
earlier than the one specified bydate.Other wise, it returnsfalse.
Object clone( ) Duplicates the invokingDateobject.
int compareTo(Datedate) Compares the value of the invoking object with that ofdate.Returns
0 if the values are equal. Returns a negative value if the invoking
object is earlier thandate.Returns a positive value if the invoking
object is later thandate.
boolean equals(Objectdate) Returnstrueif the invokingDateobject contains the same time
and date as the one specified bydate.Other wise, it returnsfalse.
long getTime( ) Returns the number of milliseconds that have elapsed since
Januar y 1, 1970.
int hashCode( ) Returns a hash code for the invoking object.
void setTime(longtime) Sets the time and date as specified bytime,which represents
an elapsed time in milliseconds from midnight, Januar y 1, 1970.
String toString( ) Converts the invokingDateobject into a string and returns the result.
TABLE 18-3 The Nondeprecated Methods Defined byDate