I need to find out the difference in days between two dates, so is there a method where I can pass two Date instances and then it will return the difference as an int? Thanks
I assume you are using the java.util.Date class.
From the API, there is a method compareTo(Date anotherDate);
int i = firstDateObject.compareTo(secondDateObject);
Hope that helps. By the way, most of the stuff in the Date class is depricated and it is preferred to use the class Calendar instead.
Thanks for the reply. I managed to get round my problem by restructuring the algorithm.
I wasn’t too sure whether the compareTo function in java.util.Date returned the days either side of the firstDateObject e.g. -2 if first date was two days before the second, or if it worked like the some of the other compareTo fuctions only returning -1 if the first date is less than the second, 0 if there equal, or 1 if the first date is greater than the second?
As u said,compareTo() returns only -1,0,-1
U can use getDate() method instead of compareTo() to get this job done.
Date d1=new Date(); //Wed May 19 23:40:16 GMT+05:30 2004
int date1=d1.getDate(); // date1=19
Date d2=new Date();
int date2=d2.getDate(); //date2=25