# Cannot coerce value into type: class java.util.date

**URL:** <https://forum.inductiveautomation.com/t/cannot-coerce-value-into-type-class-java-util-date/91255>\
**Category:** Ignition\
**Tags:** vision, scripting\
**Created:** [June 2, 2024, 11:49pm UTC](https://forum.inductiveautomation.com/t/cannot-coerce-value-into-type-class-java-util-date/91255 "2024-06-02T23:49:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kieren\_Irvine](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@Kieren\_Irvine](https://forum.inductiveautomation.com/u/Kieren_Irvine)\
**Post date:** [June 2, 2024, 11:49pm UTC](https://forum.inductiveautomation.com/t/cannot-coerce-value-into-type-class-java-util-date/91255/1 "2024-06-02T23:49:14Z")

</div>

I'm very new to ignition and I'm trying to simulate the start and end of a process and logging the data in-between and exporting to a csv.  
I have 2 buttons, one to start and one to stop. The start button assigns the current start time to a memory DateTime tag with the following script:

`system.tag.writeBlocking(["[IsoTreat]StartTime"],[system.date.now()])`

The second button then needs to use this value to find how far back to query to create the dataset, it uses the following code:

```python

EndTime = system.date.now()

DataSet = system.tag.queryTagHistory(paths =[
        "[IsoTreat]Studio5000/AddititveScaled",
        "[IsoTreat]Studio5000/PowderScaled"], 
        startDate = ["[IsoTreat]StartTime"], 
        endDate = EndTime, 
        columnNames = 'Pot 1''Pot 2', 
        returnSize = 0
)

csv = system.dataset.toCSV(DataSet)

filePath = system.file.saveFile("myExport.csv", "csv", 
        "Comma Separated Values")

if filePath:
	system.file.writeFile(filePath, csv)

```

When I press the button I get the following error:

`java.lang.ClassCastException: java.lang.ClassCastException: Cannot coerce value '['[IsoTreat]StartTime']' into type: class java.util.Date`

```
caused by ClassCastException: Cannot coerce value '
    ['[IsoTreat]StartTime']' into type: class java.util.Date

```

any help would be greatly appreciated. Thanks.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [June 2, 2024, 11:59pm UTC](https://forum.inductiveautomation.com/t/cannot-coerce-value-into-type-class-java-util-date/91255/2 "2024-06-02T23:59:09Z")

</div>

Hi, Kieren and welcome.

Please have a quick read of [Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831) and then edit your post using the pencil icon. All your indentation is lost otherwise. Thanks.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [June 3, 2024, 12:12am UTC](https://forum.inductiveautomation.com/t/cannot-coerce-value-into-type-class-java-util-date/91255/3 "2024-06-03T00:12:18Z")

</div>

Adding some air into your code makes it more readable and shows up a few issues.

```python
EndTime = system.date.now()
DataSet = system.tag.queryTagHistory(paths = [
      "[IsoTreat]Studio5000/AddititveScaled", 
      "[IsoTreat]Studio5000/PowderScaled"
    ], 
    startDate = ["[IsoTreat]StartTime"], <-- !
    endDate = EndTime, 
    columnNames = 'Pot 1''Pot 2', <-- !
    returnSize = 0
)

```

In the line,  
`startDate = ["[IsoTreat]StartTime"]`  
you are supplying a string where a datetime is required. You need to retrieve that first as a separate operation.

```python
tagStartDate = system.tag.readBlocking(["[IsoTreat]StartTime"])
DataSet = system.tag.queryTagHistory(paths = [
      "[IsoTreat]Studio5000/AddititveScaled", 
      "[IsoTreat]Studio5000/PowderScaled"
    ], 
    startDate = tagStartDate[0].value,
    endDate = EndTime,
    columnNames = ['Pot 1', 'Pot 2'],
    returnSize = 0
)

```

Note the fix on the columnNames **list**.

There may be more!

* * *

Edit: Fixed the line `startDate = tagStartDate[0].value`  
The `readBlocking` function returns a list with a _qualified value_ for each tag requested. The qualified value includes the actual value, the quality and the timestamp. Since we only read one tag it will be returned as `tagStartDate[0]` and the value can be read using `tagStartDate[0].value`. Thank you @Ujwal_Wankhede.

---

<div class="post-metadata">

**Author:** ![Kieren\_Irvine](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@Kieren\_Irvine](https://forum.inductiveautomation.com/u/Kieren_Irvine)\
**Post date:** [June 3, 2024, 12:36am UTC](https://forum.inductiveautomation.com/t/cannot-coerce-value-into-type-class-java-util-date/91255/4 "2024-06-03T00:36:09Z")

</div>

Thank you,

I've added in the changes you said, now I'm getting a similar error:

`cannot coerce value '[[Mon Jun 03 10:27:56 AEST 2024, Good, Mon Jun 03 10:27:56 AEST 2024 (1717374476887)]]' into type: class java.util.Date`

Is there maybe something wrong with the StartTime tag data type?

---

<div class="post-metadata">

**Author:** ![Ujwal\_Wankhede](https://avatars.discourse-cdn.com/v4/letter/u/4af34b/32.png) [@Ujwal\_Wankhede](https://forum.inductiveautomation.com/u/Ujwal_Wankhede)\
**Post date:** [June 3, 2024, 12:47am UTC](https://forum.inductiveautomation.com/t/cannot-coerce-value-into-type-class-java-util-date/91255/5 "2024-06-03T00:47:00Z")

</div>

Please read this tread

> [@How to obtain tag value from Qualified Value](https://forum.inductiveautomation.com/t/how-to-obtain-tag-value-from-qualified-value/62089/5):
>
> You’re requesting the tag values for a list, which is returned as a list of Qualified Values. A Qualified Value has all sorts of info including quality and the timestamp of the most recent change. You should modify your read to store only the value of the Qualified Value: ID = tag[0].value Edit: updated to reflect that you’re also getting a list of Qualified Values. Relevant code: def runAction(self, event): tags = system.tag.readBlocking(["[default]CylinderTag"]) system.perspective.print…

You should make following modification to code work  
`startDate = tagStartDate[0].value`

---

<div class="post-metadata">

**Author:** ![Kieren\_Irvine](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@Kieren\_Irvine](https://forum.inductiveautomation.com/u/Kieren_Irvine)\
**Post date:** [June 3, 2024, 1:33am UTC](https://forum.inductiveautomation.com/t/cannot-coerce-value-into-type-class-java-util-date/91255/6 "2024-06-03T01:33:30Z")

</div>

I made that change and it works now. Thanks to both of you
