jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes Problem with GregorianCalendar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem with GregorianCalendar" Watch "Problem with GregorianCalendar" New topic
Author

Problem with GregorianCalendar

Gaurav Mac Mathur
Ranch Hand

Joined: Feb 19, 2002
Posts: 47
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

Joined: Aug 20, 2001
Posts: 1817

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 ]

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Gaurav Mac Mathur
Ranch Hand

Joined: Feb 19, 2002
Posts: 47
Yes
I stand corrected...
DEC is 11th month of Year in Java
Thanks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem with GregorianCalendar
 
Similar Threads
problem: How to get last month date
Is java Date/Calendar a bug?
Calendar roll() not working
Gregorian roll() function- April Fools Bug??
Date in java