File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem with GregorianCalendar" Watch "Problem with GregorianCalendar" New topic

Problem with GregorianCalendar

Gaurav Mac Mathur
Ranch Hand

Joined: Feb 19, 2002
Posts: 47
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);
System.out.println(" Date after 31 Dec 2003 -->"+ c);
System.out.println(" Date on Day before the days after 31 Dec 2003 -->"+ c);
Am i missing something ???
:roll: :roll: :roll: :roll: :roll:
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

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
I stand corrected...
DEC is 11th month of Year in Java
I agree. Here's the link:
subject: Problem with GregorianCalendar
It's not a secret anymore!