Problem in calculating WEEK OF YEAR

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Problem in calculating WEEK OF YEAR

1. myproblem Guest

## Problem in calculating WEEK OF YEAR

I have written a code that goes like this :
(for calculating the week no. of year 2001 for date 31-12-2001)
It returns the answer as "1" (which is wrong) instead of "53".This date comes
under week no .53 for the year 2001.

SimpleDateFormat df5 = new SimpleDateFormat("dd-MM-yyyy");

Calendar cal4 = new GregorianCalendar();
String str_ey = "31-12-2001";;
java.util.Date enddateofyear = null;

enddateofyear = df5.parse(str_ey);

cal4.setTime(enddateofyear);

System.out.println("cal4 WEEK " + cal4.get(Calendar.WEEK_OF_YEAR));

2. Paul Clapham Guest

## Re: Problem in calculating WEEK OF YEAR

Here's the paragraph from the API documentation for GregorianCalendar that
describes what you are seeing:

"For example, January 1, 1998 was a Thursday. If getFirstDayOfWeek() is
MONDAY and getMinimalDaysInFirstWeek() is 4 (these are the values reflecting
ISO 8601 and many national standards), then week 1 of 1998 starts on
December 29, 1997, and ends on January 4, 1998. If, however,
getFirstDayOfWeek() is SUNDAY, then week 1 of 1998 starts on January 4,
1998, and ends on January 10, 1998; the first three days of 1998 then are
part of week 53 of 1997. "

In my case and probably yours too, getFirstDayOfWeek() is SUNDAY and
getMinimalDaysInFirstWeek() is 1, so your date is in week 1 of 2002.

PC2

"myproblem" <aartee1@rediffmail.com> wrote in message
news:3c0f1828\$1@147.208.176.211...
>
> I have written a code that goes like this :
> (for calculating the week no. of year 2001 for date 31-12-2001)
> It returns the answer as "1" (which is wrong) instead of "53".This date

comes
> under week no .53 for the year 2001.
>
>
> SimpleDateFormat df5 = new SimpleDateFormat("dd-MM-yyyy");
>
> Calendar cal4 = new GregorianCalendar();
> String str_ey = "31-12-2001";;
> java.util.Date enddateofyear = null;
>
> enddateofyear = df5.parse(str_ey);
>
> cal4.setTime(enddateofyear);
>
> System.out.println("cal4 WEEK " + cal4.get(Calendar.WEEK_OF_YEAR));

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise