• Post Reply Bookmark Topic Watch Topic
  • New Topic

GregorianDate and week_number  RSS feed

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Question about gregorian date...
I initialize a GregorianCalendar to the 4th of January of 2005 like this:
Calendar newCal = new GregorianCalendar(2005,0,4);
System.out.println("Week is =" + newCal.get(Calendar.WEEK_OF_YEAR));
it prints out
Week is =2
The problem is that the 4th of January of 2005 actually is week 1.
What am I doing wrong ???
Thanks in advance ...
Per
 
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, it is week 2. If you take a look at the javadoc for WEEK_OF_YEAR, it states that this value is dependant on getFirstDayOfWeek() and getMinimalDaysInFirstWeek(). Since 4 Jan 2005 is a Tuesday, getFirstDayOfWeek() is not figuring into this (unless you are in a locale where the first day of the week is Wednesday or some such...) getMinimalDaysInFirstWeek() will return one (1), so (again, depending on locale) the first week of 2005 is Saturday, January 1 2005 through Saturday, January 1, 2005. Sunday, 2 January 2005 through Saturday, 8 January 2005 are the second week.
Hope that this helps.
 
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the definition of week number? does a week start from Sunday or Saturday?
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd also like to point out that 31 December 2004 is also the first week of the year, as Sunday, 26 December 2004 through Friday, 31 December 2004 are "glommed" onto the first week of 2005. Again, this is all in the JavaDoc.
[ May 07, 2003: Message edited by: Joel McNary ]
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Don Liu:
what is the definition of week number? does a week start from Sunday or Saturday?

It depends on the calendar/locale. use .getFirstDayOfWeek() to find out. In Western Civilization (TM), it is ususlly either Sunday (in the States) or Monday (in France, etc.)
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the informations!
 
Per Lovdinger
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, my locale is Swedish, so I will try to
initialize a gregorian date with the swedish
locale and see if that works...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!