aspose file tools*
The moose likes Beginning Java and the fly likes date difference in days Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "date difference in days" Watch "date difference in days" New topic
Author

date difference in days

deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
dd=(int)((dat2.getTime() - dat1.getTime()) / (1000 * 60 * 60 * 24));
it returns days but problem is that
is two dates are
2010-11-22 and 2010-12-22 it returns 0
what should i do?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61180
    
  66

Please use real words when posting to the forums. Abbreviations such as "diff" in place of "difference" only serve to make your posts more difficult to read and less likely to generate useful responses.

Please click this link ⇒ UseRealWords for more information.

I have adjusted the title for you.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

How are you casting to an int in JavaScript?

(Moving from HTML/JavaScript to Beginning Java.)
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I can't reproduce this; if I have different dates, I don't get 0 for the difference between the times.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18821
    
  40

David Newton wrote:I can't reproduce this; if I have different dates, I don't get 0 for the difference between the times.


Agreed. If the two days were only a day apart, then I would have made the argument that maybe the hours, minutes, and seconds, along with rounding down, may be the cause. But the example given is a month apart... something else is happening here.

Maybe you can show us the code that initializes dat1 and dat2?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
these code is written in jsp page not in javascript
java.util.Date dat1,dat2 ;
formatter = new SimpleDateFormat("yy-mmm-dd");
dat1 = (java.util.Date)formatter.parse("2010-11-22");
dat2 = (java.util.Date)formatter.parse("2010-12-22");
Jimi Svedenholm
Ranch Hand

Joined: May 19, 2001
Posts: 53
amrita singhal wrote:



Here lies your problem. The lower case 'm' stands for minutes, not month. You should use upper case 'M' instead. And you should only use two 'M's, not three, since three (ie 'MMM') means the month spelled out as a word instead of a number.

Try this instead:


As a tip for future troubleshooting. When you encounter problems, always try to print out the different variable values in your code, to see if they are what you expect. If you would have printed out the dat1 and dat2 values you would have seen that the dates were wrong, thus you would know that the bug is in the parsing of the date.
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
Jimi Svedenholm

thankyou so much
you explained it very well

and also thankyou to all people
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: date difference in days