This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Java in General and the fly likes Calendar roll() not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Java in General
Bookmark "Calendar roll() not working" Watch "Calendar roll() not working" New topic

Calendar roll() not working

Judy Herilla
Ranch Hand

Joined: Jan 05, 2001
Posts: 89
I am getting a Calendar representing today - March 1st, 2001 and rolling it back one day. I am not ending up with Feb 28th, but March 31st. This has worked in changing from January to Fevruary, but now I am having this problem. I read in the API that GregorianCalendar should resolve this issue, but I saw the same results - March 31st, 2001.
Any thoughts on how to fix this? Besides writing code to handle February?
GregorianCalendar newCal = new GregorianCalendar();
System.out.println("Todays calendar date:" + getStringFromCalendar(newCal));
//roll back the number of days between the dates passed in
newCal.roll(Calendar.DATE, -1);
System.out.println("After rollback New calendar date:" + getStringFromCalendar(newCal));

Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Johanna,
Try to use Calendar.DAY_OF_YEAR instead. It will work every time in the roll method.
NOTE: According to API: Calendar.DATE is equivalent to Calendar.DAY_OF_MONTH which makes sense with yours results!
[This message has been edited by Manfred Leonhardt (edited March 01, 2001).]
Judy Herilla
Ranch Hand

Joined: Jan 05, 2001
Posts: 89
Thanks Manfred, that did the trick!!!
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Calendar roll() not working
It's not a secret anymore!