Results 1 to 4 of 4
  1. #1
    du-it is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Calendar DAY_OF_YEAR problem in 1.6?

    I have a problem using the GregorianCalendar:
    First I create a GregorianCalendar instance and set the day of year with value -30. That's fine.
    Afterwards I uses another GregorianCalendar doing the same but setting the day of year with value -1 and the result totally differs from what I expected. Instead of subtracting one day of the GregorianCalendar's current date it sets the calendar to 30th Dec one year before:

    Java Code:
    GregorianCalendar gc = new GregorianCalendar();
    gc.add(GregorianCalendar.DAY_OF_YEAR, -30);
    Date startDate = gc.getTime();
    System.out.println("startDate: " + startDate);               // Sat Feb 04 10:01:21 CET 2012
    Date endDate = new Date();
    System.out.println("endDate: " + endDate);
    GregorianCalendar gc1 = new GregorianCalendar();
    gc1.set(Calendar.DAY_OF_YEAR, -1);
    Date endDate = gc1.getTime();
    System.out.println("endDate: " + endDate);                 //  Thu Dec 30 10:01:24 CET 2011
    Where am I going wrong??

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: Calendar DAY_OF_YEAR problem in 1.6?

    add != set

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    du-it is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Re: Calendar DAY_OF_YEAR problem in 1.6?


    Thank you!!!

    Oha.. for the first date I did it well. (Sometimes I miss the forest for the trees.)

    What is the difference between add and set in this case? Is it because the value was negative? I can't see why the result is 64. (I would have an idea of what happened if the result would be 364. Then I could guess that Calendar calculates MAX_DAYS_OF_A_YEAR plus (myMinusValue) = 364.)

    However, it works now. Thank you.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: Calendar DAY_OF_YEAR problem in 1.6?

    What is the difference between add and set in this case?
    In this case or any other, read the API to find what the method promises to deliver.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. problem in calendar component
    By irakli_dev in forum NetBeans
    Replies: 0
    Last Post: 12-09-2011, 11:08 AM
  2. Calendar mutator problem
    By WimHaar in forum New To Java
    Replies: 7
    Last Post: 01-04-2011, 12:25 PM
  3. Problem with compile Calendar
    By santunez in forum New To Java
    Replies: 1
    Last Post: 05-06-2010, 12:36 AM
  4. Calendar date problem
    By groobelar in forum New To Java
    Replies: 2
    Last Post: 01-17-2010, 03:47 AM
  5. Problem with calendar
    By Felissa in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 08:39 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •