This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

finding date diff  RSS feed

 
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
Posts: 21047
85
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
Posts: 21047
85
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!