Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date

 
Jeff Grant
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how about:
or something like that.
Jamie
 
Jeff Grant
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Jeff Grant
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic