• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

GregorianDate and week_number

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the definition of week number? does a week start from Sunday or Saturday?
 
Joel McNary
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the informations!
 
Per Lovdinger
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
I've been selected to go to the moon! All thanks to this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic