Week calculation

S

smreddy

Hi,

I want to get a list of week numbers given a date range. Please help me
out.
For ex
Start Date Jan 1 2006
End Date Feb 28 2006
O/p it should give W12006 W22006 ...... W92006

Thanks for the help
Reddy
 
Z

zero

Hi,

I want to get a list of week numbers given a date range. Please help me
out.
For ex
Start Date Jan 1 2006
End Date Feb 28 2006
O/p it should give W12006 W22006 ...... W92006

Thanks for the help
Reddy

Calendar:get(Calendar.WEEK_OF_YEAR)
Calendar:get(Calendar.YEAR)
 
R

Roedy Green

Start Date Jan 1 2006
End Date Feb 28 2006
O/p it should give W12006 W22006 ...... W92006

I have four possible solutions for you all based around BigDate

BigDate is available at http://mindprod.com/products1..html#COMMON11
-------------
BigDate has a method getISOWeekNumber to tell you the ISO week number
of a given day. But I suspect you have a different definition of week
from ISO. It is defined like this:

Get week number 1 to 53 of the year this date falls in, according to
the rules of ISO standard IS-8601 section 5.5. A week that lies partly
in one year and partly in another is assigned a number in the year in
which most of its days lie. This means that week 1 of any year is the
week that contains 4 January, or equivalently week 1 of any year is
the week that contains the first Thursday in January. Most years have
52 weeks, but years that start on a Thursday and leap years that start
on a Wednesday have 53 weeks. Jan 1 may well be in week 53 of the
previous year! Only defined for dates on or after 1600 Jan 01. You can
find out how many ISO weeks there are per year with new BigDate( year,
12,
31).getISOWeekNumber();

@return week number 1..53, 0 for null or invalid date.
http://www.pip.dknet.dk/~pip10160/calendar.faq3.txt
Calendar FAQ
--------
BigDate also has a method getWeekNumber:
defined like this:
Get week number 1 to 53 of the year this date falls in. This does NOT
follow the rules of ISO standard IS-8601 section 5.5. Week 1 is the
first week with any days in the current year. Weeks start on Sunday.
Jan 1 and Dec 31 are always considered part of the current year. Only
defined for dates on or after 1600 Jan 01.
@return week number 1..53, 0 for null or invalid date.
--------------
Bigdate has a method getDayOfWeek that will tell you the day of the
week of any given day (Calendar has a similar feature with the added
complication of deciding a timezone and time of day).
Get day of week for given ordinal. Is it zero-based starting with
Sunday. @return day of week 0=Sunday 1=Monday 2=Tuesday 3=Wednesday
4=Thursday 5=Friday 6=Saturday WARNING: not compatible with
1=Calendar.SUNDAY

-----------
BigDate also has a method toOrdinal that will give you days since
1970. If you divide that by 7 it will tell you weeks since then, if
you do a % modulus 7 it will tell you the day of week.

I suspect the code you want might then look like this:

int ordinalLast = BigDate.toOrdinal( yyyy2, mm2, dd2 );

int ordinalFirst = BigDate.toOrdinal ( yyyy1, mm1, dd1);

int weekLast = (ordinallast + fiddle) / 7;

int weekFirst = (ordinalFirst + fiddle) / 7;

where fiddle is a number chosen to fiddle the results to start weeks
on your preferred day and adjust week 0 to the desired epoch..
Here weeks are numbered from some epoch, not starting over each year.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top