Results 1 to 15 of 15
Thread: Calendar bug?
- 02-03-2009, 01:57 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 2
- Rep Power
- 0
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
- 02-03-2009, 02:20 PM #2
Affirmative. Very strange ! More focused code
Result: 2008 1 364Java 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));
code "cal.set(2008, 11, 28)" works well (2008 52 363).Last edited by ProjectKaiser; 02-03-2009 at 02:24 PM.
-
Likely it's not a bug, it's a feature. ;)
edit: run this:
and you'll get: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); } }
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.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
Last edited by Fubarable; 02-03-2009 at 06:55 PM.
- 02-04-2009, 07:02 AM #4
- 02-04-2009, 07:41 AM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 02-04-2009, 09:26 AM #6
Member
- Join Date
- Feb 2009
- Posts
- 2
- Rep Power
- 0
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
- 02-04-2009, 09:43 AM #7
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
are you sure?
it works for some specific year only
- 02-04-2009, 09:54 AM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 02-04-2009, 10:25 AM #9
- 02-04-2009, 10:35 AM #10
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
- 02-04-2009, 10:42 AM #11
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-7Last edited by ProjectKaiser; 02-04-2009 at 10:46 AM.
- 02-04-2009, 10:57 AM #12
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 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.
- 02-08-2009, 05:36 AM #13
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.
- 02-08-2009, 06:41 AM #14
here's the homepage: Joda - Next Generation Java
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-08-2009, 09:56 AM #15
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Similar Threads
-
Calendar Pop-up issue
By jeeva in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 01-29-2009, 01:09 AM -
calendar
By John in forum SWT / JFaceReplies: 12Last Post: 08-07-2008, 10:54 PM -
How to use Calendar class
By Java Tip in forum java.utilReplies: 0Last Post: 04-04-2008, 02:33 PM -
Design Calendar UI
By praveen.kb in forum AWT / SwingReplies: 0Last Post: 01-21-2008, 11:54 AM -
Web calendar
By Daniel in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 06-27-2007, 05:36 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks