# Java Dates

**URL:** <https://forums.visual-paradigm.com/t/java-dates/8300>\
**Category:** Uncategorized\
**Created:** [April 29, 2005, 12:00am UTC](https://forums.visual-paradigm.com/t/java-dates/8300 "2005-04-29T00:00:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Peters](https://avatars.discourse-cdn.com/v2/letter/m/fbc32d/32.png) [@Mike\_Peters](https://forums.visual-paradigm.com/u/Mike_Peters)\
**Post date:** [October 21, 2003, 12:00am UTC](https://forums.visual-paradigm.com/t/java-dates/8300/1 "2003-10-21T00:00:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![martyn](https://avatars.discourse-cdn.com/v2/letter/m/a6a055/32.png) [@martyn](https://forums.visual-paradigm.com/u/martyn)\
**Post date:** [October 21, 2003, 12:00am UTC](https://forums.visual-paradigm.com/t/java-dates/8300/2 "2003-10-21T00:00:00Z")

</div>

Hi Mike,

I assume you are using the java.util.Date class.

From the API, there is a method compareTo(Date anotherDate);

Usage:

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.

Martyn

---

<div class="post-metadata">

**Author:** ![MikePeters](https://avatars.discourse-cdn.com/v2/letter/m/e274bd/32.png) [@MikePeters](https://forums.visual-paradigm.com/u/MikePeters)\
**Post date:** [October 21, 2003, 12:00am UTC](https://forums.visual-paradigm.com/t/java-dates/8300/3 "2003-10-21T00:00:00Z")

</div>

Hi Martyn,

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?

thanks

Mike

---

<div class="post-metadata">

**Author:** ![senthil](https://avatars.discourse-cdn.com/v2/letter/s/c89c15/32.png) [@senthil](https://forums.visual-paradigm.com/u/senthil)\
**Post date:** [May 20, 2004, 12:00am UTC](https://forums.visual-paradigm.com/t/java-dates/8300/4 "2004-05-20T00:00:00Z")

</div>

Mike Peter,

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();  
d2.setDate(25);  
int date2=d2.getDate(); //date2=25  
System.out.println(date2-date1); //result=6

senthil
