Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with GregorianCalendar

 
Gaurav Mac Mathur
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
The requirement is to roll ahead one day and then roll back to same day.
if i use rolll function, only date is rolled and not Month
If i use add function,I am not able to roll back the year using the add(-1).
GregorianCalendar c= new GregorianCalendar(2003,12,31);
System.out.println(" Date on 31 Dec 2003 -->"+ c);
c.add(Calendar.DATE,+1);
System.out.println(" Date after 31 Dec 2003 -->"+ c);
c.add(Calendar.DATE,-1);
System.out.println(" Date on Day before the days after 31 Dec 2003 -->"+ c);
Am i missing something ???
Regards
Gaurav
:roll: :roll: :roll: :roll: :roll:
 
Joel McNary
Bartender
Posts: 1824
Eclipse IDE Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes--months are 0 indexed! When you say:
new GregorianCalendar(2003,12,31);
you are specifying the 31st day of the thirteenth month of 2003--Which is Jan 31, 2004.
Change to
new GregorianCalendar(2003,11,31);
to get the desired result.
(Or use new GregorianCalendar(2003, Claendar.DECEMBER, 31))
(And use SimpleDateFormats to more easily display the dates; that's how I spotted the problem:

[ July 08, 2003: Message edited by: Joel McNary ]
 
Gaurav Mac Mathur
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes
I stand corrected...
DEC is 11th month of Year in Java
Thanks
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic