Date class in Java (With Examples)
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.
Current date is Tue Jul 12 18:35:37 IST 2016
Date represented is Wed Jan 28 02:50:23 IST 1970
- 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.
Date d1 =
Date d2 =
Date d3 =
a = d3.after(d1);
"Date d3 comes after "
"date d2: "
b = d3.before(d2);
"Date d3 comes before "
"date d2: "
c = d1.compareTo(d2);
"Miliseconds from Jan 1 "
"1970 to date d1 is "
"Before setting "
"After setting "
Date d3 comes after date d2: true
Date d3 comes before date d2: false
Miliseconds from Jan 1 1970 to date d1 is 60935500800000
Before setting Tue Jul 12 13:13:16 UTC 2016
After setting Fri Jun 25 21:50:33 UTC 1976
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course
and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks