Results 1 to 15 of 15

Thread: Calendar bug?

  1. #1
    Stigvig is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default Calendar bug?

    Hello!
    Can anyone explane this?

    Java Code:
    Calendar cal = Calendar.getInstance();
    for (int i = 0;i<10;i++){
      System.out.println(cal.get(Calendar.YEAR) + " " + cal.get  (Calendar.WEEK_OF_YEAR));
      cal.add(Calendar.WEEK_OF_YEAR,-1);
    }
    
    2009 6
    2009 5
    2009 4
    2009 3
    2009 2
    2008 1
    2008 52
    2008 51
    2008 50
    2008 49

  2. #2
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

    Default

    Affirmative. Very strange ! More focused code
    Java Code:
            cal.set(2008, 11, 29);
            System.out.println(cal.get(Calendar.YEAR) 
               + " " + cal.get(Calendar.WEEK_OF_YEAR)  
               + " " + cal.get(Calendar.DAY_OF_YEAR));
    Result: 2008 1 364

    code "cal.set(2008, 11, 28)" works well (2008 52 363).
    Last edited by ProjectKaiser; 02-03-2009 at 02:24 PM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Likely it's not a bug, it's a feature. ;)

    edit: run this:
    Java Code:
      public static void main(String[] args)
      {
        Calendar cal = Calendar.getInstance();
        for (int i = 0; i < 10; i++)
        {
          System.out.printf("%4s %2d %30s%n", 
            cal.get(Calendar.YEAR), 
            cal.get(Calendar.WEEK_OF_YEAR),
            cal.getTime());
          cal.add(Calendar.WEEK_OF_YEAR, -1);
        }
      }
    and you'll get:
    Java Code:
    2009  6   Tue Feb 03 12:56:30 EST 2009
    2009  5   Tue Jan 27 12:56:30 EST 2009
    2009  4   Tue Jan 20 12:56:30 EST 2009
    2009  3   Tue Jan 13 12:56:30 EST 2009
    2009  2   Tue Jan 06 12:56:30 EST 2009
    2008  1   Tue Dec 30 12:56:30 EST 2008
    2008 52   Tue Dec 23 12:56:30 EST 2008
    2008 51   Tue Dec 16 12:56:30 EST 2008
    2008 50   Tue Dec 09 12:56:30 EST 2008
    2008 49   Tue Dec 02 12:56:30 EST 2008
    Per the API, the first week of the year starts with 1. And if you study the output, you'll see that the first week of the year starts the week indicated, but regardless the date of the calendar object itself is in a different year. So there are no bugs here.
    Last edited by Fubarable; 02-03-2009 at 06:55 PM.

  4. #4
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    So there are no bugs here.
    Not quite sure what you mean.
    If you put "2008 Dec 30" to Calendar and ask for week of year it gives you 1.

    You really think that last week of the year has number one ?

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    The last/first week of the year is determined slightly differently in differeing locales. But, generally, if Thursday falls in the new year, then it is week 1 (regardless of whether you are referring to the last day or two of the old year or the first day or two of the new year), if it falls in the previous year, then it is week 53 (and would also be referred to as "new year" 53 for the day or two remaining in the week for the new year).

    It is not, I repeat, not a bug. It is the proper handling method.

    Edit: And, this is not "new" with the onset of computers, or anything. It has been this way for a long, long time, just it doesn't really interest most people (until they are confronted with it), and most people are not taught that it is this way (once again, until they are confronted with it, and start to scream about it, usually in combination with some program, or programming language, and claiming it to be a bug).

    Edit again: And I know someone is going to scream "But there are only 52 weeks in a year". Yes, that's true, there are only 52 full weeks in a year, but what is 52 * 7 and how many days are there in a year? Do you see a discrepancy here? And in time keeping a "week number" applies to the entire week, so, either you always have week 53 flow over into the new year, or you break the last rule and "split" the week into two weeks, or you lay out a rule to determine whether the week gets 53 or 1, and that's what was decided.
    Last edited by masijade; 02-04-2009 at 07:50 AM.

  6. #6
    Stigvig is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default

    Thanks for the answers. A solution to my problem must be to set the day of week to Sunday.

    Java Code:
    Calendar cal = Calendar.getInstance();
    cal.setFirstDayOfWeek(Calendar.MONDAY);
    cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    for (int i = 0;i<10;i++){				
     cal.get(Calendar.YEAR);
     cal.get(Calendar.WEEK_OF_YEAR);
     cal.add(Calendar.WEEK_OF_YEAR,-1);
     System.out.println(cal.get(Calendar.YEAR) + " " + cal.get(Calendar.WEEK_OF_YEAR)
    );				
    }
    
    
    2009 5
    2009 4
    2009 3
    2009 2
    2009 1
    2008 52
    2008 51
    2008 50
    2008 49
    2008 48

  7. #7
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    In some locales Sunday is the first day of week, in some it is the last (seemingly in yours, it is the last). Since Thursday is the day that decides it, I would suggest using Thursday, and even that won't work for some locales.

    Better would be to simply not print the line when the week number is one but the DAY_OF_YEAR is greater than 7 or the Week number is 52 or 53 and the DAY_OF_YEAR is less than 7.

  9. #9
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Better would be to simply not print the line when the week number is one but the DAY_OF_YEAR is greater than 7
    In this case I would print 53 as week number... rather then skipping the line I charge customer money for :) ( in due time I was sending timesheets per week and week number had to be mentioned )

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote Originally Posted by ProjectKaiser View Post
    In this case I would print 53 as week number... rather then skipping the line I charge customer money for :) ( in due time I was sending timesheets per week and week number had to be mentioned )
    Then you should be using 2009 1, IMHO, rather than 2008 53 (which doesn't exist legally).

  11. #11
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

    Default

    You are right
    ISO week date - Wikipedia, the free encyclopedia

    2008-12-31 is 2009-W01-3

    ... BUT !!!!

    2009-12-31 is 2009-W53-4

    and even

    2010-01-03 is 2009-W53-7
    Last edited by ProjectKaiser; 02-04-2009 at 10:46 AM.

  12. #12
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote Originally Posted by ProjectKaiser View Post
    You are right
    ISO week date - Wikipedia, the free encyclopedia

    2008-12-31 is 2009-W01-3

    ... BUT !!!!

    2009-12-31 is 2009-W53-4

    and even

    2010-01-03 is 2009-W53-7
    Correct, because Thursday of those two weeks fall in the "old" year, so Week 53 exists and Week 1 doesn't start until the first day of the next week (when that is depends on the locale, but would either be Sunday or Monday). It's perfectly in line with the standard.

  13. #13
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    Default

    Java's Date/Calendar APIs are lame. Too much legacy code depends on them working they way they do, but Java should throw them all out and start over.

    Look at Jodadate, google for it.

  14. #14
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    here's the homepage: Joda - Next Generation Java
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  15. #15
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Unfortunately, using Joda won't change the way the week number is determined. ;-)

Similar Threads

  1. Calendar Pop-up issue
    By jeeva in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 01-29-2009, 01:09 AM
  2. calendar
    By John in forum SWT / JFace
    Replies: 12
    Last Post: 08-07-2008, 10:54 PM
  3. How to use Calendar class
    By Java Tip in forum java.util
    Replies: 0
    Last Post: 04-04-2008, 02:33 PM
  4. Design Calendar UI
    By praveen.kb in forum AWT / Swing
    Replies: 0
    Last Post: 01-21-2008, 11:54 AM
  5. Web calendar
    By Daniel in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 06-27-2007, 05:36 PM

Posting Permissions

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