The perfect place for easy learning...

Java Programming

×

Topics List


Date class in java





The Date is a built-in class in java used to work with date and time in java. The Date class is available inside the java.util package. The Date class represents the date and time with millisecond precision.

The Date class implements Serializable, Cloneable and Comparable interface.

🔔 Most of the constructors and methods of Date class has been deprecated after Calendar class introduced.

The Date class in java has the following constructor.

S. No. Constructor with Description
1 Date( )

It creates a Date object that represents 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.

3 Date(int year, int month, int date) - Depricated

It creates a date object with the specified year, month, and date.

4 Date(int year, int month, int date, int hrs, int min) - Depricated

It creates a date object with the specified year, month, date, hours, and minuts.

5 Date(int year, int month, int date, int hrs, int min, int sec) - Depricated

It creates a date object with the specified year, month, date, hours, minuts and seconds.

5 Date(String s) - Depricated

It creates a Date object and initializes it so that it represents the date and time indicated by the string s, which is interpreted as if by the parse(java.lang.String) method.

The Date class in java has the following methods.

S.No. Methods with Description
1 long getTime()

It returns the time represented by this date object.

2 boolean after(Date date)

It returns true, if the invoking date is after the argumented date.

3 boolean before(Date date)

It returns true, if the invoking date is before the argumented date.

4 Date from(Instant instant)

It returns an instance of Date object from Instant date.

5 void setTime(long time)

It changes the current date and time to given time.

6 Object clone( )

It duplicates the invoking Date object.

7 int compareTo(Date date)

It compares current date with given date.

8 boolean equals(Date date)

It compares current date with given date for equality.

9 int hashCode()

It returns the hash code value of the invoking date object.

10 Instant toInstant()

It converts current date into Instant object.

11 String toString()

It converts this date into Instant object.

Let's consider an example program to illustrate methods of Date class.

Example
import java.time.Instant;
import java.util.Date;

public class DateClassExample {

	public static void main(String[] args) {

		Date time = new Date();
		
		System.out.println("Current date => " + time);
		
		System.out.println("Date => " + time.getTime() + " milliseconds");
		
		System.out.println("after() => " + time.after(time) + " milliseconds");
		
		System.out.println("before() => " + time.before(time) + " milliseconds");
		
		System.out.println("hashCode() => " + time.hashCode());

	}

}

When we execute the above code, it produce the following output.

Date class in java