How to get a proper week number and MATCHING year number

Java classes such as SimpleDateFormat offer convenient means to find out the correct week number of a given date. Week numbering is locale specific and not always intuitive. For example, Dec 31, 2001 is in week 1 of 2002 in locales where a week starts with Monday or Sunday. Now, if one formats the date using a method such as:

new SimpleDateFormat("'week: 'ww', year: 'yyyy").format(theDate);

this produces result

week: 1, year:2001

when the typically anticipated value would be "week: 1, year:2002".

Is there any means to find out the year number, which matches to the given week number for a date? Or do I have to create a kludge myself?

I would be suprised if this feature is not part of Java API, since it seems like a quite common need. Then again, I'm not suprised by surprises in Java API anymore ;)

[871 byte] By [jimpo3a] at [2007-10-2 10:57:15]
# 1
Simply set the Calendar to the week's start prior to outputting.
da.futta at 2007-7-13 3:23:26 > top of Java-index,Java Essentials,Java Programming...
# 2

Could you explain a bit more? I am not sure you understood the problem?

In the example above, what I want displayed is "week: 1, year: 2002". how is setting Calendar to week's start going to help, since week starts in 2001? Although the week starts in 2001, correct week number is 1/2002.

jimpo3a at 2007-7-13 3:23:26 > top of Java-index,Java Essentials,Java Programming...
# 3

> Could you explain a bit more? I am not sure you

> understood the problem?

>

> In the example above, what I want displayed is

> "week: 1, year: 2002". how is setting Calendar to

> o week's start going to help, since week starts in

> 2001? Although the week starts in 2001, correct week

> number is 1/2002.

Ah, I thought you wanted it the other way around.

Well, then simply set the Calendar to the weeks end. If I understood your explanation correctly, the first week of the year is that of the first sunday of the year, isn't it ?

da.futta at 2007-7-13 3:23:26 > top of Java-index,Java Essentials,Java Programming...
# 4

Well, the real problem is just that the rules determining which year the week belongs to are complicated. In some locales, weeks start with Monday, in some Sunday. In some locales year that has 4 or more days of the week "owns" the week, in some locales there might be different rules.

All this complex logic is already implemented in Calendar / SimpleDateFormat / etc classes, so it would be clearly valuable thing to be able to use that code. For example, Calendar.get(YEAR_OF_WEEK_OF_YEAR) or similar. But, it does not seem to be possible.

As I mentioned originally, I can implement my own "kludge" to do this if I have to. This is not nice, since I will be duplicating the functionality already implemented in the calendar classes. Of course, solution is neither as simple as "set calendar to the weeks end" or "set calendar to the weeks start"...

jimpo3a at 2007-7-13 3:23:26 > top of Java-index,Java Essentials,Java Programming...