GregorianCalendar add, roll question

Hi people,

I'm having a problem with the GC add function. Here's what I'm trying to do. I have a date such as 2001-01-01 when I run date.add(Calendar.DATE, 1), I get 2001-01-02...great, no problem.

However, when I run date.add(Calendar.DATE, -1), I get 2001-0-31...any ideas as to why or what I can do to fix this?

Thank you,

Shrubz

[374 byte] By [shrubz] at [2007-9-26 2:10:13]
# 1

I'm not sure how you're implementing the add() function (i.e., I don't know what "date" refers to in your question). I wrote this little program and it worked:

import java.util.*;

public class Test {

public static void main (String args[]) {

GregorianCalendar testCal = new GregorianCalendar();

testCal.set(Calendar.MONTH,0);

testCal.set(Calendar.DATE,1);

Date testDate = testCal.getTime();

System.out.println(testDate);

testCal.add(Calendar.DATE,-1);

testDate = testCal.getTime();

System.out.println(testDate);

}

}

Here's the output:

Mon Jan 01 12:17:28 PST 2001

Sun Dec 31 12:17:28 PST 2000

Rich

rmiller1985 at 2007-6-29 9:00:49 > top of Java-index,Archived Forums,Java Programming...
# 2
Are you using Calendar.get(Calendar.MONTH) to display the months portion of your date? If so, then you need to know that the so-called month values returned by this method are 0=January, 1=February, and so on. This "feature" has been voted into the "10 Most Idiotic Things About Java".
DrClap at 2007-6-29 9:00:49 > top of Java-index,Archived Forums,Java Programming...
# 3
Haha...You're 100% right, it is the most idiotic in java so far...Apparently that is the problem, I am using Calendar.MONTH. Thanks to you all.Shrubz
shrubz at 2007-6-29 9:00:49 > top of Java-index,Archived Forums,Java Programming...
# 4
I'd like to see the rest of the ballot!Rich
rmiller1985 at 2007-6-29 9:00:49 > top of Java-index,Archived Forums,Java Programming...