By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 2,122 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

calendar.add() : I'M FED UP !

P: n/a
Hello all,

I do the following :
SimpleDateFormat df = new
SimpleDateFormat("yyyy-MM-dd");
Calendar d = new GregorianCalendar(2004,11,30);
d.add(Calendar.DATE, 7);
dt=d.getTime();
System.out.println("Date:"+df.format(dt));

Can someone explain me why I get "Date:2005-01-06" and not
"Date:2004-12-07" as a normal person would expect ?

Thank you very much in advance.
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
nos

"vigi98" <vi****@free.fr> wrote in message
news:c1**************************@posting.google.c om...
Hello all,

I do the following :
SimpleDateFormat df = new
SimpleDateFormat("yyyy-MM-dd");
Calendar d = new GregorianCalendar(2004,11,30);
d.add(Calendar.DATE, 7);
dt=d.getTime();
System.out.println("Date:"+df.format(dt));

Can someone explain me why I get "Date:2005-01-06" and not
"Date:2004-12-07" as a normal person would expect ?


So this means that if there is an error
in your program, that
you are not normal !

Jul 17 '05 #2

P: n/a
"vigi98" <vi****@free.fr> ha scritto nel messaggio news:c1**************************@posting.google.c om...

Hi,
first of all, sorry for my english.
Calendar d = new GregorianCalendar(2004,11,30);
The problem is month: 11 is equivalent to December, because the month (only the month) is 0 based indexing:

0 -> Genuary
1 -> Febrary
....
11 -> December
Thank you very much in advance.


Good Bye,
GianpieroP
Jul 17 '05 #3

P: n/a
Thanks Gianpiero, it was simple... Programming is sometime so
desperating...
Anyway, your answer solve my problem and don't care about your
English, mine is not better.
Jul 17 '05 #4

P: n/a
"vigi98" <vi****@free.fr> wrote in message
news:c1**************************@posting.google.c om...
Hello all,

I do the following :
SimpleDateFormat df = new
SimpleDateFormat("yyyy-MM-dd");
Calendar d = new GregorianCalendar(2004,11,30);
d.add(Calendar.DATE, 7);
dt=d.getTime();
System.out.println("Date:"+df.format(dt));

Can someone explain me why I get "Date:2005-01-06" and not
"Date:2004-12-07" as a normal person would expect ?

Thank you very much in advance.


Two reasons.
- You haven't read the javadoc for java.util.Calendar.
you should be using Calendar.getInstance(), not GregorianCalendar().

- You haven't read the javadoc for
java.util.GregorianCalendar#GregorianCalendar(int, int, int)
"month - the value used to set the MONTH time field in the calendar. Month
value is 0-based. e.g., 0 for January."

It pays to read the appropriate documentation for the technology that you
are using.
--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.