File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Weeks in a year Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Weeks in a year" Watch "Weeks in a year" New topic
Author

Weeks in a year

Kudret Serin
Ranch Hand

Joined: Aug 02, 2005
Posts: 167

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
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
Kudret Serin
Ranch Hand

Joined: Aug 02, 2005
Posts: 167


Thats what i need. Thank you both for the posts.
 
 
subject: Weeks in a year
 
Similar Threads
Extract a number using regular expression?
Cleared SCJP 1.4 with 85%
jquest question!!!!!!!!
MATH Problem - Shor.MIN_VALUE
2004 - looking back.........