Bug in Calendar class? it shows 31st feb 2006

Discussion in 'Java' started by hemant.singh@gmail.com, Mar 6, 2006.

  1. Guest

    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 = calNow.getTimeInMillis();
    options = 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);
    }

    }
     
    , Mar 6, 2006
    #1
    1. Advertising

  2. VisionSet Guest

    <> wrote in message
    news:...
    > 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
     
    VisionSet, Mar 6, 2006
    #2
    1. Advertising

  3. 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.
     
    Jaakko Kangasharju, Mar 6, 2006
    #3
  4. Vova Reznik Guest

    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.

    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 = calNow.getTimeInMillis();
    > options = 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);
    > }
    >
    > }
    >
     
    Vova Reznik, Mar 6, 2006
    #4
  5. VisionSet Guest

    "VisionSet" <> wrote in message
    news:XiVOf.29223$...
    >
    > <> wrote in message
    > news:...
    > > 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
     
    VisionSet, Mar 6, 2006
    #5
  6. P.Hill Guest

    Jaakko Kangasharju wrote:
    >
    > So remove that -1.
    >


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

    -Paul
     
    P.Hill, Mar 7, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Amit Aronovitch

    OSDC::Israel Conference 26-28 Feb. 2006

    Amit Aronovitch, Nov 21, 2005, in forum: Python
    Replies:
    0
    Views:
    276
    Amit Aronovitch
    Nov 21, 2005
  2. w chun
    Replies:
    0
    Views:
    275
    w chun
    Dec 7, 2005
  3. Georg Brandl

    Bug Day on Friday, 31st of March

    Georg Brandl, Mar 20, 2006, in forum: Python
    Replies:
    0
    Views:
    280
    Georg Brandl
    Mar 20, 2006
  4. Georg Brandl
    Replies:
    0
    Views:
    245
    Georg Brandl
    Mar 29, 2006
  5. Tim Sutherland
    Replies:
    1
    Views:
    113
    Tim Sutherland
    Aug 9, 2006
Loading...

Share This Page