• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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: 22787
131
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: 22787
131
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic