This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes finding date diff Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "finding date diff" Watch "finding date diff" New topic
Author

finding date diff

vijay kumar
Ranch Hand

Joined: Nov 10, 2006
Posts: 62
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

Joined: Oct 27, 2005
Posts: 19655
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

You can do something like this.


SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
vijay kumar
Ranch Hand

Joined: Nov 10, 2006
Posts: 62
Thanks a lot
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finding date diff
 
Similar Threads
Handling java.sql.Timestamp
Yesterdays Date
Database access for orinary people, ordinary PCs
javascript calculating between 2 dates
difference in days