The class Date represents a specific instant in time, with millisecond precision. The Date class of java.util package implements Serializable, Cloneable and Comparable interface. It provides constructors and methods to deal with date and time with java.
Constructors
- Date() : Creates date object representing current date and time.
- Date(long milliseconds) : Creates a date object for the given milliseconds since January 1, 1970, 00:00:00 GMT.
- Date(int year, int month, int date)
- Date(int year, int month, int date, int hrs, int min)
- Date(int year, int month, int date, int hrs, int min, int sec)
- Date(String s)
Note : The last 4 constructors of the Date class are Deprecated.
Output:
Current date is Tue Jul 12 18:35:37 IST 2016
Date represented is Wed Jan 28 02:50:23 IST 1970
Important Methods
- boolean after(Date date) : Tests if current date is after the given date.
- boolean before(Date date) : Tests if current date is before the given date.
- int compareTo(Date date) : Compares current date with given date. Returns 0 if the argument Date is equal to the Date; a value less than 0 if the Date is before the Date argument; and a value greater than 0 if the Date is after the Date argument.
- long getTime() : Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
- void setTime(long time) : Changes the current date and time to given time.