File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with adding months to Gregorian Calender

 
Farakh khan
Ranch Hand
Posts: 833
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Farakh khan
Ranch Hand
Posts: 833
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Its fine now.

Thanks & best regards
 
Jagdeep Sharma
Ranch Hand
Posts: 121
IBM DB2 Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 833
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for correcting Line 5

Appreciated

Best regards
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic