How to extract WEEK_OF_YEAR from java.util.date?

I’m sure that this is easy for python and java gurus in this community, but I’m struggling with it…

I need to get WEEK OF YEAR from date/time which I get from the Popup Calendar component in Vision.
On the screen there are two Popup Calendars so the user can choose the start and end date for showing data from SQL database in the Power table.
No problem with that.

But then I have some tables in the db, where the data are stored with the week of year number and the user should be able to choose to show data according to week numbers.

So I thought I would use the same Popup calendars date property to extract week of year number from that date. But it turns out that (at least for me) that’s not as simple as it seems…

So I have this:

start_date = event.source.parent.getComponent('calStartDate').date
end_date = event.source.parent.getComponent('calEndDate').date

How can I get the week number out of this…?
I need something like this:

start_date = event.source.parent.getComponent('calStartDate').date
end_date = event.source.parent.getComponent('calEndDate').date
start_week = int( ....week_of_year(start_date))
end_week = int(....week_of_year(end_date))

Have a look at java.util.calendar and this example.

Hi @chi, thanks

I don’t wanna be rude, but that was one of my first attempts to do it.
I have searched through the internet and stackoverflow.com for examples but…

When I try this I don’t get the correct week number:


Today is 26. August 2021 and the week number is 34, not 35.

That’s one problem.

Another is that I don’t know how to put the date from my Popup calendar (which is java.util.date) )to the Calendar (which is java.util.GregorianCalendar)…?

java calander starts counting 1-1 as week 1.

Sorry, but I don’t follow you…? :flushed:

so its week 35 acording to java i guess, idk
also you can set the date from calender with

javaDate=getpopupvalue
cal = Calendar.getInstance()
cal.setTime(javaDate)

actualy the hell it says1-1-2021 is week 3? xd

OK, this is confusing…
According to the internet:

According to the PostgreSQL:
week_number2

And according to the Java(Ignition):

And according to the Excel:

What the heck is going on…? :confused:

i guess some start at 0 and others at 1… for java calender i suppose you’ll have to do -1 if you want to be consistent with the internet

ive noticed they start at day1 too while most arrays start at 0 xd :exploding_head:

The problem is that I’m reading data from PostgresSQL where week number is 34, and in Ignition I get 35. So when the user chooses todays date it’ll look for week number 35 in the PostgreSQL, which doesn’t exist (yet… until next week according to PostgreSQL)…?

Lots of programming languages treat things like this differently. It just means you need to be aware of the differences and convert between them if using them together. Not much you can do, unless there’s an option to change the default in the config

you’ll have to also reduce the week number by when when you filter i guess
i would create a project script with a similar syntax like ‘getWeekOfYear(date)’ that does it for you, then you only need to call that and never need to do all the calender stuff and you wont have to remeber to do -1 every time

OK. Apparently, there are two systems for WEEK numbering: non-ISO and ISO
SYSTEM 1 (non-ISO):
The week containing January 1 is the first week of the year, and is numbered week 1.
SYSTEM 2 (ISO):
The week containing the first Thursday of the year
is the first week of the year, and is numbered as week 1.
This system is the methodology specified in ISO 8601,
which is commonly known as the European week numbering system.

So, because I’m in Europe, I want ISO week numbering.
In Ignition you have to do:

from java.util import Calendar

cal = Calendar.getInstance()
cal.setMinimalDaysInFirstWeek(4);
cal.setFirstDayOfWeek(Calendar.MONDAY);

start_date= event.source.parent.getComponent('calZacetniDatum').date
end_date = event.source.parent.getComponent('calKoncniDatum').date

cal.setTime(start_date)
start_week_number = cal.get(Calendar.WEEK_OF_YEAR)
print "start_week_number= ", start_week_number

cal.setTime(end_date )
end_week_number = cal.get(Calendar.WEEK_OF_YEAR)
print "end_week_number= ", end_week_number

#Use start_week_number and end_week_number for filtering the results in query...

PostgreSQL apparently uses ISO week numbering by default:

select extract(week from '2021-08-26'::date); -->34
select date_part('week'::text, '2021-08-26'::date); -->34
SELECT to_char(DATE '2021-08-26', 'IW')::int; --> 34

-->If you want non-ISO results, use
SELECT to_char(DATE '2021-08-26', 'WW')::int; --> 35

In EXCEL:

#non-ISO week number:
=WEEKNUM(A3)

#ISO week number:
=ISOWEEKNUM(A3)

-----Being a programmer is the best job in the world.-------- :sunglasses:

1 Like

java.time was added to overcome the inconsistency of Calendar.

from java.time import ZoneId
from java.time.temporal import IsoFields

now = system.date.now()

date = now.toInstant().atZone(ZoneId.systemDefault()).toLocalDate()

week = date.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR)

print now
print date
print week

5 Likes