• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Weeks in a year

 
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 ]
 
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
 
Wanderer
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!