• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

date difference in days

 
deep raj
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you casting to an int in JavaScript?

(Moving from HTML/JavaScript to Beginning Java.)
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't reproduce this; if I have different dates, I don't get 0 for the difference between the times.
 
Henry Wong
author
Marshal
Pie
Posts: 21489
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
deep raj
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jimi Svedenholm

thankyou so much
you explained it very well

and also thankyou to all people
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic