Last Updated: 06 March, 2023
The class Date represents a specific instant in time with millisecond precision. The Date class was introduced in the JDK 1.0 release and is available in the java.util package.
The java.util.Date class provides many constructors and methods to deal with date and time in Java.
Date Class declaration:
The java.util.Date class extends Object class and implements the Serializable, Cloneable, and Comparable
The java.util.Date class is inherited by java.sql.Date, java.sql.Time and java.sql.Timestamp interfaces in Java.
No. | Constructor with Description |
---|---|
1. | Date() It creates a Date object that represents 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. |
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. |
6. | 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. |
Example: Date class constructors implementation
Output
Current Date & Time: Sat Mar 04 12:21:48 IST 2023
Date represented is Sun Jan 25 19:15:21 IST 1970
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 invocation date is after the argumented date. |
3. | boolean before(Date date) It returns true if the invocation date is before the argument date. |
4. | Date from(Instant instant) It returns an instance of the Date object from an instant date. |
5. | void setTime(long time) It changes the current date and time to the given time. |
6. | Object clone( ) It duplicates the invoking Date object. |
7. | int compareTo(Date date) It compares the current date with the given date. |
8. | boolean equals(Date date) It compares the current date with the given date for equality. |
9. | int hashCode() It returns the hash code value of the invoking date object. |
10. | Instant toInstant() It converts the current date into an instant object. |
11. | String toString() It converts this date into an instant object. |
Example: Date class methods implementation
Output
Date date3 comes after date date2: true
Date date3 comes before date date2: false
1
Miliseconds from Jan 1 1970 to date date1 is: 61085817000000
Before setting: Sat Mar 04 12:49:10 IST 2023
After setting: Sat Jun 26 03:20:33 IST 1976
Cloned date4: Tue Sep 26 00:00:00 IST 3905
date1.equals(date4): true
date1.equals(date2): false
About java.sql.Date Class
In Java, there is another Date class that is available in the java.sql package. The java.sql.Date class extends the java.util.Date class.
The java.sql.Date is used to represent SQL DATE, which keeps years, months, and days. No timing data is kept.
The java.sql.Date class should only be used while dealing with databases, and it does not hold information about the time zone.
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
Related Articles