File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes comparision between two dates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "comparision between two dates" Watch "comparision between two dates" New topic

comparision between two dates


Joined: May 02, 2006
Posts: 20
I have two dates one is start date and the other one is end date.
how to find which one is greater among the two dates? and if end date is less than start date i want to display one error message.

please any one help regarding the date validations.
thanks in advance
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
1) getTime()

2) before()

[ October 14, 2006: Message edited by: sven studde ]
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Moved to Java In General (Intermediate)

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi cowboys,

the Date class implements Comparable, so it is also possible to use the compareTo method.

Sven's probosal with the getTime() is more precise as you can obtain the exact milliseconds between the Date objects.
Anyway, when you want to determine days, hours and so on between the dates, you should get an instance of the Calendar class (also in java.util) and work with this.

[ October 14, 2006: Message edited by: Burkhard Hassel ]

all events occur in real time
I agree. Here's the link:
subject: comparision between two dates
It's not a secret anymore!