Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Bug in Calendar class? it shows 31st feb 2006

Reply
Thread Tools

Bug in Calendar class? it shows 31st feb 2006

 
 
hemant.singh@gmail.com
Guest
Posts: n/a
 
      03-06-2006
please run the following code
Here im creating a GregorianCalendar instance and than add one 27 times
to DAY_OF_MONTH and to my surprise it will show you 31st feb 2006
before moving to march, ,this is really crazy, any1 know of workaround?


public static void main(String[] args)
{
System.out.println("Hello World!");

Calendar calNow = new GregorianCalendar();
String WEEK[] = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
String MONTH[] = {"January", "Feburary", "March", "April", "May",
"June", "July", "August", "September", "October", "November",
"December"};
String[] options = new String[27];
long values[] = new long [ options.length ];
for (int i = 0; i < options.length; i++ ) {
values[i] = calNow.getTimeInMillis();
options[i] = WEEK[calNow.get(Calendar.DAY_OF_WEEK) - 1]+
", " + calNow.get(Calendar.DAY_OF_MONTH) +
" " + MONTH[ calNow.get(Calendar.MONTH) - 1] +
" " + calNow.get(Calendar.YEAR) ;

calNow.add(Calendar.DAY_OF_MONTH,1);
}
for (int i = 0;i < options.length; i++ )
{
System.out.println(options[i]);
}

}

 
Reply With Quote
 
 
 
 
VisionSet
Guest
Posts: n/a
 
      03-06-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> please run the following code
> Here im creating a GregorianCalendar instance and than add one 27 times
> to DAY_OF_MONTH and to my surprise it will show you 31st feb 2006
> before moving to march, ,this is really crazy, any1 know of workaround?


setLenient(false)

--
Mike W


 
Reply With Quote
 
 
 
 
Jaakko Kangasharju
Guest
Posts: n/a
 
      03-06-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

> please run the following code
> Here im creating a GregorianCalendar instance and than add one 27 times
> to DAY_OF_MONTH and to my surprise it will show you 31st feb 2006
> before moving to march, ,this is really crazy, any1 know of workaround?


Month values in Calendar are 0-based so there is no need to subtract 1
to index into your array. You are actually printing the days for
March, just printing the month as February.

> " " + MONTH[ calNow.get(Calendar.MONTH) - 1] +


So remove that -1.

--
Jaakko Kangasharju, Helsinki Institute for Information Technology
Warner Books classifies The Far Side as non-fiction. Creepy.
 
Reply With Quote
 
Vova Reznik
Guest
Posts: n/a
 
      03-06-2006
Calendar.JANUARY = 0

Your program prints February instead of March.
You need to change line

MONTH[ calNow.get(Calendar.MONTH) - 1]
to
MONTH[ calNow.get(Calendar.MONTH)]

I know, it is very confusing, because
Calendar.SUNDAY = 1, not 0.

(E-Mail Removed) wrote:
> please run the following code
> Here im creating a GregorianCalendar instance and than add one 27 times
> to DAY_OF_MONTH and to my surprise it will show you 31st feb 2006
> before moving to march, ,this is really crazy, any1 know of workaround?
>
>
> public static void main(String[] args)
> {
> System.out.println("Hello World!");
>
> Calendar calNow = new GregorianCalendar();
> String WEEK[] = {"Sunday", "Monday", "Tuesday", "Wednesday",
> "Thursday", "Friday", "Saturday"};
> String MONTH[] = {"January", "Feburary", "March", "April", "May",
> "June", "July", "August", "September", "October", "November",
> "December"};
> String[] options = new String[27];
> long values[] = new long [ options.length ];
> for (int i = 0; i < options.length; i++ ) {
> values[i] = calNow.getTimeInMillis();
> options[i] = WEEK[calNow.get(Calendar.DAY_OF_WEEK) - 1]+
> ", " + calNow.get(Calendar.DAY_OF_MONTH) +
> " " + MONTH[ calNow.get(Calendar.MONTH) - 1] +
> " " + calNow.get(Calendar.YEAR) ;
>
> calNow.add(Calendar.DAY_OF_MONTH,1);
> }
> for (int i = 0;i < options.length; i++ )
> {
> System.out.println(options[i]);
> }
>
> }
>

 
Reply With Quote
 
VisionSet
Guest
Posts: n/a
 
      03-06-2006

"VisionSet" <(E-Mail Removed)> wrote in message
news:XiVOf.29223$(E-Mail Removed)...
>
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
> > please run the following code
> > Here im creating a GregorianCalendar instance and than add one 27 times
> > to DAY_OF_MONTH and to my surprise it will show you 31st feb 2006
> > before moving to march, ,this is really crazy, any1 know of workaround?

>
> setLenient(false)
>


Sorry, I was in a rush, thought it would help, it solves the opposite
problem.

--
Mike W


 
Reply With Quote
 
P.Hill
Guest
Posts: n/a
 
      03-07-2006
Jaakko Kangasharju wrote:
>
> So remove that -1.
>


I would also suggest
SimpleDateFormat
and the use of:
Calendar.JANUARY thru Calendar.DECEMBER

-Paul

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ruby Weekly News 31st July - 6th August 2006 Tim Sutherland Ruby 1 08-09-2006 12:24 PM
Reminder: Bug Day this Friday, 31st of March Georg Brandl Python 0 03-29-2006 02:52 PM
Bug Day on Friday, 31st of March Georg Brandl Python 0 03-20-2006 02:28 PM



Advertisments