Velocity get weeknumber (extended ISO 8601)

Hi all,

I am using this simple code to get the weeknr

{{velocity}}
#set($weeknr = $datetool.format('w',$xwiki.getDate()))
$weeknr
{{/velocity}}

But this code returns “2”, which is not the correct weeknumber by the extended ISO 8601 (which would return “1”)

What do I need to change to get the correct weeknumber?

Thanks!

There’s nothing wrong here, see GregorianCalendar (Java Platform SE 7 ) (even if not intuitive at first sight, but that’s how this java api works).

Note that it’s better to use:

{{velocity}}
#set($weeknr = $datetool.format('w', $datetool.date))
$weeknr
{{/velocity}}

since $xwiki.getDate() is deprecated.

1 Like

Hi Vincent,

Thanks for the notice that $wiki.getDate() is deprecated, I have changed my code to use $datetool.date

Now for my problem:

By the Gregorian Calendar, today is week 2
By extended ISO 8601, today is week 1

In the Netherlands, we use the ISO 8601 numbering of weeks, so I need to get the ISO 8601 weeknumber. Any idea how?

My reading of GregorianCalendar (Java Platform SE 7 ) is that it’s following ISO 8601.

I can’t help you more, you’d need to ask the JDK developers :wink:

EDIT: I assume you’ve read the link and tried to change getFirstDayOfWeek() and did some tests in java with that, right?

I am now :slight_smile:

I meant:

The week determination is compatible with the ISO 8601 standard when getFirstDayOfWeek() is MONDAY and getMinimalDaysInFirstWeek() is 4, which values are used in locales where the standard is preferred. These values can explicitly be set by calling setFirstDayOfWeek() and setMinimalDaysInFirstWeek().

What I’m not sure, is how you could set this using the $datetool velocity tool (i.e. ComparisonDateTool (VelocityTools 3.2-SNAPSHOT API)).

BTW there’s even simpler code:

{{velocity}}
#set($weeknr = $datetool.get('w'))
$weeknr
{{/velocity}}

Yeah, I have been looking around in the $datetool, but I can’t find any function like getFirstDayofWeek or setFirstDayOfWeek or setMinimalDaysInFirstWeek :frowning:

Seems like it’s missing something?