• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Problem with adding months to Gregorian Calender

 
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Its fine now.

Thanks & best regards
 
Ranch Hand
Posts: 121
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for correcting Line 5

Appreciated

Best regards
 
reply
    Bookmark Topic Watch Topic
  • New Topic