jQuery in Action, 2nd edition*
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
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: 19723
    
  20

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: 19723
    
  20

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: finding date diff