aspose file tools*
The moose likes Java in General and the fly likes Problem with adding months to Gregorian Calender Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem with adding months to Gregorian Calender" Watch "Problem with adding months to Gregorian Calender" New topic
Author

Problem with adding months to Gregorian Calender

Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Hello,



OUTPUT:
======
Today's Date: 29-10-2010
After Six Months: 29-05-2010

I am trying to get accurate results of the six months later date i.e. 28-05-2011 why its giving me the above wrong output?

Best regards
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

This doesn't change anything. It retrieves the same year and day, but instead of the month it retrieves the day of the week in the month (Calendar.MONTH = 2, Calendar.DAY_OF_WEEK_IN_MONTH = 8 so 6 more than Calendar.MONTH).

What you want to do is modify the calendar to add (hint, hint) 6 months, then retrieve the 3 values again.

Oh, and the months are 0-based; Calendar.JANUARY is 0. To make the months 1-based use the following:
This will always work, even if Calendar.JANUARY would ever change to 1 (which it won't).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732


Its fine now.

Thanks & best regards
Jagdeep Sharma
Ranch Hand

Joined: May 24, 2010
Posts: 121

Farakh khan wrote:Hello,



OUTPUT:
======
Today's Date: 29-10-2010
After Six Months: 29-05-2010

I am trying to get accurate results of the six months later date i.e. 28-05-2011 why its giving me the above wrong output?

Best regards



Can't we use cal.add(Calendar.Month, 6);
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Farakh khan wrote:Its fine now.

Thanks & best regards

Line 5 still will return 10 instead of 11, but the adding is fine like this. So you're welcome


Jagdeep Sharma wrote:Can't we use cal.add(Calendar.Month, 6);

Yes we can Well, if you turn Month into MONTH.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Thanks for correcting Line 5

Appreciated

Best regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with adding months to Gregorian Calender