Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What's the problem with Calendar/GregorianCalendar!?

229 views
Skip to first unread message

Nicholas

unread,
Feb 13, 2001, 4:11:18 AM2/13/01
to
Hi,

I'm banging my head on this one. PLEASE HELP!! And many thanks!

I have a loop which i'm trying to add 1 day per loop. The start date is
20010101 (Jan 1st, 2001 ). But the problem is that after adding the first
day, the result i get is always 20010202 instead of 20010102! Pseudo code as
follows:

//------start of code------
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
cal.set(2001,1,1);

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
sdf.setCalendar( Calendar.getInstance(TimeZone.getDefault()) );

Calendar papaNewCal = (Calendar)cal.clone();

for( int i=0; i<someSize; i++ ) {
Calendar sonNewCal = (Calendar)papaNewCal.clone();
sonNewCal.add(Calendar.DAY_OF_YEAR,1);

String newDate = sdf.format(sonNewCal.getTime());

System.out.println("The new date is " + newDate); // shows '20010202
after adding the first time.

papaNewCal = sonNewCal;
}
// -------end--------


Regards,
Nicholas


Jon Skeet

unread,
Feb 13, 2001, 4:55:22 AM2/13/01
to
Nicholas <So_Ex...@excite.com> wrote:
> Hi,
>
> I'm banging my head on this one. PLEASE HELP!! And many thanks!
>
> I have a loop which i'm trying to add 1 day per loop. The start date is
> 20010101 (Jan 1st, 2001 ). But the problem is that after adding the first
> day, the result i get is always 20010202 instead of 20010102! Pseudo code as
> follows:
>
> //------start of code------
> Calendar cal = Calendar.getInstance(TimeZone.getDefault());
> cal.set(2001,1,1);

That's the problem. From the documentation to Calendar.set:

"month - the value used to set the MONTH time field. Month value is 0-
based. e.g., 0 for January."

--
Jon Skeet - sk...@pobox.com
http://www.pobox.com/~skeet

Paul Hill

unread,
Feb 13, 2001, 8:48:42 AM2/13/01
to Nicholas
Nicholas wrote:
> cal.set(2001,1,1);

As John already pointed out 1 == February, next time
consider the use of:

cal.set( 2001, Calendar.January, 1 );

> SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

FWIW, When debugging date calculations, it is often useful to
build yourself a really detailed SimpleDateFormat and use it
to print out the before and after values. If you had done
this in this case (or even used your actual format twice),
you would have seen Feb 1 go to Feb 2 and the solution would
have jumped out at you.

> sdf.setCalendar( Calendar.getInstance(TimeZone.getDefault()) );

Checking the documentation, you will find that this step is
not needed.

> Calendar papaNewCal = (Calendar)cal.clone();

Why do you keep cloning?

>
> for( int i=0; i<someSize; i++ ) {
> Calendar sonNewCal = (Calendar)papaNewCal.clone();
> sonNewCal.add(Calendar.DAY_OF_YEAR,1);

You want the old one in the exactly the state you left it,
just use it, forget the clone. But maybe you are actually
trying to build 365 different calendars. What is it you
want all of these calendars for?

-Paul

--
Myriad Genetics: http://www.myriad.com/
Java FAQ: http://www.afu.com/javafaq.html (Section 9, Computer Dating)

Nicholas

unread,
Feb 13, 2001, 8:31:42 PM2/13/01
to
Well, I ehhh, can't think of an excuse.....guess, I was very frustrated and
started altering the codes any 'ol how just to see what happens.....hee,
hee.

Thanks Paul for the pointers.

"Paul Hill" <ph...@myriad.com> wrote in message
news:3A893B3A...@myriad.com...

Roedy Green

unread,
Feb 15, 2001, 1:51:57 AM2/15/01
to
On Tue, 13 Feb 2001 17:11:18 +0800, "Nicholas" <So_Ex...@excite.com>
wrote or quoted :

>I have a loop which i'm trying to add 1 day per loop. The start date is
>20010101 (Jan 1st, 2001 ). But the problem is that after adding the first
>day, the result i get is always 20010202 instead of 20010102! Pseudo code as
>follows:


See BigDate in the Java glossary. It was designed to be much easier
to use than GregorianCalendar.


--

Please consult the JAVA GLOSSARY for almost any
Java-related question.
If the answer is not there, COMPLAIN!
See http://mindprod.com/jgloss.html
or http://209.153.246.39/jgloss.html

Answers to the six most Frequently Asked Questions:

1) for conversion problems, see "conversion". .
2) for I/O problems, see the "File I/O Amanuensis" and "File".
3) if you want to create a *.exe, see "native compiler".
4) if you are new to Java, see "getting started".
5) to spawn an external program see "exec".
6) See "error messages" in the Java glossary.
--
Roedy Green, Canadian Mind Products
Custom computer programming since 1963

0 new messages