Calendar.roll on december,31 !!!!

G

Gianni

My application has this terrible bug and now is time to fix!
It works 364 days in the year but not on dec,31

I have to add one day to the "today".

In fact on 2004-dec-31 I have 2004-jan-1

I use :
Calendar calendar = Calendar.getInstance(Locale.GERMANY);
calendar.roll(6, 1);

but obviously does not work on dec,31... any idea how to fix it???
Thanks
Gianni
 
M

Michael Borgwardt

Gianni said:
My application has this terrible bug and now is time to fix!
It works 364 days in the year but not on dec,31

I have to add one day to the "today".

In fact on 2004-dec-31 I have 2004-jan-1

I use :
Calendar calendar = Calendar.getInstance(Locale.GERMANY);
calendar.roll(6, 1);

but obviously does not work on dec,31...

Sure it does. It gives the expected result according to the
specification in the API doc. Why do you expect something else,
or rather: what do you expect?
 
T

Thomas Weidenfeller

Gianni said:
I have to add one day to the "today".

In fact on 2004-dec-31 I have 2004-jan-1

I use :
Calendar calendar = Calendar.getInstance(Locale.GERMANY);
calendar.roll(6, 1);

but obviously does not work on dec,31...

It works according to the specification. What about spending a minute or
two and reading the API documentation?
any idea how to fix it???

By using the method which is supposed to do what you want to do.

BTW: I think it is rather uncool to use the number literal 6 instead of
the predefined constant to address the field.

/Thomas
 
A

Alex Hunsley

Thomas said:
It works according to the specification. What about spending a minute or
two and reading the API documentation?



By using the method which is supposed to do what you want to do.

BTW: I think it is rather uncool to use the number literal 6 instead of
the predefined constant to address the field.

It's definitely error prone. The constants allow you to not worry about
whether monday or sunday is the first day, and if the first day is 0 or
1, etc. la, la....
 
A

Alex Kay

My application has this terrible bug and now is time to fix!
It works 364 days in the year but not on dec,31

I have to add one day to the "today".

In fact on 2004-dec-31 I have 2004-jan-1

I use :
Calendar calendar = Calendar.getInstance(Locale.GERMANY);
calendar.roll(6, 1);

but obviously does not work on dec,31... any idea how to fix it???

Hi,

1. It isn't good practice to say "6" say Calendar.DAY_OF_YEAR ;-)
2. the second parameter is a boolean not an integer
3. the following may not be perfect but it works.

Calendar cal = Calendar.getInstance (Locale.GERMANY);

// Set to 31-Dec-2004
cal.set (2004, 11, 31, 0, 0, 0);

/*
* Problem. cal.roll (Calendar.DAY_OF_YEAR, true);
* fails to handle 31-Dec as desired
* the year doesn't change.
*
* A solution. Roll time forward by 24 hours in
* terms of milliseconds.
*/
cal.setTimeInMillis( cal.getTimeInMillis() + (24*60*60*1000));

System.out.println (new Date(cal.getTimeInMillis()));

4. I just have my own methods like this. They works across day light
saving too (which is often where things break).

static void nextDay (Calendar cal)
{
cal.setTimeInMillis( cal.getTimeInMillis() + (24*60*60*1000));
}

// And or
static void nextDay (Date d) {
d.setTime( d.getTime() + (24*60*60*1000));
}

Hope that helps
Alex Kay
 
A

Alex Kay

My application has this terrible bug and now is time to fix!
It works 364 days in the year but not on dec,31

I have to add one day to the "today".

In fact on 2004-dec-31 I have 2004-jan-1

I use :
Calendar calendar = Calendar.getInstance(Locale.GERMANY);
calendar.roll(6, 1);

but obviously does not work on dec,31... any idea how to fix it???

OOPS I made a mistake in my previous post, the 2nd parameter for roll
can be an integer or a boolean, please accept my apology. Clearly I
don't use roll much.

And the one-line method I suggested last time was part of another
routine of mine which I need for another more complex purpose but in
your case you can probably just use the 'add' method:

Calendar cal = Calendar.getInstance (Locale.GERMANY);
cal.set (2004, 11, 31, 0, 0, 0);
cal.add (Calendar.DAY_OF_YEAR, 1);

It unlike roll will also increase 'larger fields' as required.

Once again sorry about any confusion I may have caused.
Alex Kay
 
D

Dag Sunde

Gianni said:
My application has this terrible bug and now is time to fix!
It works 364 days in the year but not on dec,31

I have to add one day to the "today".

In fact on 2004-dec-31 I have 2004-jan-1

I use :
Calendar calendar = Calendar.getInstance(Locale.GERMANY);
calendar.roll(6, 1);

but obviously does not work on dec,31... any idea how to fix it???

In addition to all the other hints, why not use:

Calendar calendar = Calendar.getInstance(Locale.GERMANY);
calendar.add(Calendar.DATE, 1);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top