java.util.Date Class

The java.util.Date class represents date and time in java. It provides constructors and methods to deal with date and time in java.

The java.util.Date class implements Serializable, Cloneable and Comparable<Date> interface. It is inherited by java.sql.Date, java.sql.Time and java.sql.Timestamp interfaces.

After Calendar class, most of the constructors and methods of java.util.Date class has been deprecated. Here, we are not giving list of any deprecated constructor and method.

java.util.Date Class Constructors

No.ConstructorDescription
1)Date()It creates a date object representing the current date and time.
2)Date(long milliseconds)It creates a date object for the given milliseconds since January 1, 1970, 00:00:00 GMT.

java.util.Date Class Methods

No.MethodDescription
1)boolean after(Date date)It tests if current date is after the given date.
2)boolean before(Date date)It tests if current date is before the given date.
3)Object clone()It returns the clone object of current date.
4)int compareTo(Date date)It compares current date with given date.
5)boolean equals(Date date)It compares current date with given date for equality.
6)static Date from(Instant instant)It returns an instance of Date object from Instant date.
7)long getTime()It returns the time represented by this date object.
8)int hashCode()It returns the hash code value for this date object.
9)void setTime(long time)It changes the current date and time to given time.
10)Instant toInstant()It converts current date into Instant object.
11)String toString()It converts this date into Instant object.

java.util.Date Example

Let's see the example to print date in Java using java.util.Date class.

Example-1

DateExample.java

Output:

Wed Mar 27 08:22:02 IST 2015

Example-2

DateExample1.java

Output:

Wed Mar 27 08:22:02 IST 2015