Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finding date diff

 
vijay kumar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a problem with finding date def.
if i submit two dates i need to get the dif value between days if it is more than 30 days some alert message should be thrown.
please help me in this.

Regards,
Vijay.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If they are in the same year it's easy:

I'll leave it up to you how to handle the case where the years are different. Hint: it includes cal1.get(Calendar.YEAR) to get the year, cal1.add(Calendar.YEAR, 1) to add one year and cal1.getActualMaximum(Calendar.DAY_OF_YEAR) to get the number of days in the calendar's current year.
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do something like this.
 
vijay kumar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except that code does not handle leap years well. That's why I suggested using getActualMaximum instead, but you have to change the year for that too. That's what the add is for.

Also keep in mind that the second date can be before the first date.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic