wood burning stoves*
The moose likes Beginning Java and the fly likes Date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Date" Watch "Date" New topic
Author

Date

Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
It doesn't seem like there is a built in function... does anyone have a simple way to compare two Date of type Date in order to see how many days apart they are?
I was thinking, perhaps convert them to Juilen and do a subtraction to get the remainder between the two. But that might run into problems when dealing with when the year changes.
Anyone have any suggestions?
Thanks, in advance!
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

how about:
or something like that.
Jamie
Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169

Thanks for the code segment. It works quite often, but when you go between months, it has issues. I don't know why, it looks as though it should work.
The sample code sends 28.0 to my command window. Thoughts on this? It should send back 31.0.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Jeff,
While it's arguably confusing, "[a] month is represented by an integer form 0 to 11; 0 is January, 1 is February, and so forth; thus 11 is December" (Date class documentation).


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
Oh my gosh! What was I thinking? How could I ever imagine that something would start with 1 instead of 0?? Geeze, now I feel dumb. Thanks to both of you for your help. I can't believe that I didn't realize what was wrong when I did a Date.getMonth() and it was one higher than what I initialized it to. :roll: I thought it was strange, that should have tipped me off.
THANKS!!!
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Originally posted by Jeff Grant:
Oh my gosh! What was I thinking? How could I ever imagine that something would start with 1 instead of 0?? Geeze, now I feel dumb. Thanks to both of you for your help. I can't believe that I didn't realize what was wrong when I did a Date.getMonth() and it was one higher than what I initialized it to. :roll: I thought it was strange, that should have tipped me off.
THANKS!!!
It still gets me every once in a while too! Just to make sure that it doesn't happen in production I use the the static variables for setting the month values --> cal.set( 2001, Calendar.DECEMBER, 25 ); No misinterprations allowed when using the static variables! It may also help any newbies that have to maintain your code.
Jamie
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Date