Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Weeks in a year

 
Kudret Serin
Ranch Hand
Posts: 167
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
How can i get the number of weeks in a year? Usually there are 52 weeks in a year but for example in 2006 there are 53 weeks.
Is there any way to check it?
I have to change this code because 52 is hardcoded:

Thanks in advance
[ January 28, 2007: Message edited by: Kudret Serin ]
 
Henry Wong
author
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kudret Serin:
Hi All,
How can i get the number of weeks in a year? Usually there are 52 weeks in a year but for example in 2006 there are 53 weeks.
Is there any way to check it?


Considering that there are 365 days in a year (one day more than 52 times 7), doesn't every year have a 53rd week?

Anyway, never tried this myself, but would this work?



Henry
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately

    c.getMaximum(Calendar.WEEK_OF_YEAR)

will always return 53, the maximum possible value for any year. However

    c.getActualMaximum(Calendar.WEEK_OF_YEAR)

will return 52 or 53, the maximum for the *current* year (whatever c has been set to). This is probably what is desired here, but it depends on how you want to count weeks. It may be useful to call

    s.setFirstDayOfWeek(Calendar.MONDAY)

if you want each week to start on a Monday (if you don't like assuming your locale's default is valid).

I recommend testing the results carefully, and if you don't agree with them, tell us specific examples where you think the result should be different. There are several subtly different ways that one might count weeks.
[ January 28, 2007: Message edited by: Jim Yingst ]
 
Kudret Serin
Ranch Hand
Posts: 167
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thats what i need. Thank you both for the posts.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic