# How to extract WEEK\_OF\_YEAR from java.util.date?

**URL:** <https://forum.inductiveautomation.com/t/how-to-extract-week-of-year-from-java-util-date/50310>\
**Category:** Ignition\
**Tags:** ignition80, scripting, vision\
**Created:** [August 26, 2021, 5:26am UTC](https://forum.inductiveautomation.com/t/how-to-extract-week-of-year-from-java-util-date/50310 "2021-08-26T05:26:26Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![zxcslo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zxcslo/32/61610_2.png) [@zxcslo](https://forum.inductiveautomation.com/u/zxcslo)\
**Post date:** [August 26, 2021, 5:26am UTC](https://forum.inductiveautomation.com/t/how-to-extract-week-of-year-from-java-util-date/50310/1 "2021-08-26T05:26:26Z")

</div>

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:

```auto
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:

```auto
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))

```

---

<div class="post-metadata">

**Author:** ![chi](https://avatars.discourse-cdn.com/v4/letter/c/0ea827/32.png) [@chi](https://forum.inductiveautomation.com/u/chi)\
**Post date:** [August 26, 2021, 6:11am UTC](https://forum.inductiveautomation.com/t/how-to-extract-week-of-year-from-java-util-date/50310/2 "2021-08-26T06:11:27Z")

</div>

Have a look at [java.util.calendar](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html) and [this example](https://stackoverflow.com/questions/10278410/java-get-week-of-year-for-given-a-date).

---

<div class="post-metadata">

**Author:** ![zxcslo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zxcslo/32/61610_2.png) [@zxcslo](https://forum.inductiveautomation.com/u/zxcslo)\
**Post date:** [August 26, 2021, 7:06am UTC](https://forum.inductiveautomation.com/t/how-to-extract-week-of-year-from-java-util-date/50310/3 "2021-08-26T07:06:53Z")

</div>

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](http://stackoverflow.com) for examples but…

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

 ![java_calendar](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/a/8a72391b0a7771308595c3c633a135d729556d61.png)  
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_calendar2](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/7/f7e7592243a3b5bf738cec233a475a4ba0117c31.png)

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [August 26, 2021, 7:17am UTC](https://forum.inductiveautomation.com/t/how-to-extract-week-of-year-from-java-util-date/50310/4 "2021-08-26T07:17:30Z")

</div>

> [@zxcslo](#):
>
> week number is 34, not 35.

java calander starts counting 1-1 as week 1.

---

<div class="post-metadata">

**Author:** ![zxcslo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zxcslo/32/61610_2.png) [@zxcslo](https://forum.inductiveautomation.com/u/zxcslo)\
**Post date:** [August 26, 2021, 7:24am UTC](https://forum.inductiveautomation.com/t/how-to-extract-week-of-year-from-java-util-date/50310/5 "2021-08-26T07:24:33Z")

</div>

Sorry, but I don’t follow you…? 😳

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [August 26, 2021, 7:26am UTC](https://forum.inductiveautomation.com/t/how-to-extract-week-of-year-from-java-util-date/50310/6 "2021-08-26T07:26:46Z")

</div>

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

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

```

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

---

<div class="post-metadata">

**Author:** ![zxcslo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zxcslo/32/61610_2.png) [@zxcslo](https://forum.inductiveautomation.com/u/zxcslo)\
**Post date:** [August 26, 2021, 7:55am UTC](https://forum.inductiveautomation.com/t/how-to-extract-week-of-year-from-java-util-date/50310/7 "2021-08-26T07:55:42Z")

</div>

OK, this is confusing…  
According to the internet:

 ![week_number1](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/9/b9e6f54f72920630f60c06a03862e8b8933cb98a.png)

According to the PostgreSQL:  
 ![week_number2](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/f/1f1914d698484c1a4df82e78f4b85243bc2ded8d.png)

And according to the Java(Ignition):

 ![week_number3](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/c/5c44fd338c87994f6310e96a9f6407f3455ce4b5.png)

And according to the Excel:

 ![week_number4](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/1/21fe85076fdbdeb1e916568c671852e5b048450d.png)

What the heck is going on…? 😕

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [August 26, 2021, 7:59am UTC](https://forum.inductiveautomation.com/t/how-to-extract-week-of-year-from-java-util-date/50310/8 "2021-08-26T07:59:30Z")

</div>

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 🤯

---

<div class="post-metadata">

**Author:** ![zxcslo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zxcslo/32/61610_2.png) [@zxcslo](https://forum.inductiveautomation.com/u/zxcslo)\
**Post date:** [August 26, 2021, 8:23am UTC](https://forum.inductiveautomation.com/t/how-to-extract-week-of-year-from-java-util-date/50310/9 "2021-08-26T08:23:56Z")

</div>

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)…?

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [August 26, 2021, 9:56am UTC](https://forum.inductiveautomation.com/t/how-to-extract-week-of-year-from-java-util-date/50310/10 "2021-08-26T09:56:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [August 26, 2021, 9:58am UTC](https://forum.inductiveautomation.com/t/how-to-extract-week-of-year-from-java-util-date/50310/11 "2021-08-26T09:58:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zxcslo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zxcslo/32/61610_2.png) [@zxcslo](https://forum.inductiveautomation.com/u/zxcslo)\
**Post date:** [August 26, 2021, 10:23am UTC](https://forum.inductiveautomation.com/t/how-to-extract-week-of-year-from-java-util-date/50310/12 "2021-08-26T10:23:46Z")

</div>

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:

```auto
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:

```auto
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:

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

#ISO week number:
=ISOWEEKNUM(A3)

```

-----_Being a programmer is the best job in the world._-------- 🕶

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [August 26, 2021, 1:04pm UTC](https://forum.inductiveautomation.com/t/how-to-extract-week-of-year-from-java-util-date/50310/13 "2021-08-26T13:04:41Z")

</div>

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

```python
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

```

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/8/28df2f5e826de1d4308176d851431c30565d2ae9.png)
