# 1st Arg Can't be Coerced to Java.Util.Date

**URL:** <https://forum.inductiveautomation.com/t/1st-arg-cant-be-coerced-to-java-util-date/72537>\
**Category:** Ignition\
**Tags:** ignition81, perspective, scripting\
**Created:** [April 17, 2023, 6:46pm UTC](https://forum.inductiveautomation.com/t/1st-arg-cant-be-coerced-to-java-util-date/72537 "2023-04-17T18:46:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Wade](https://avatars.discourse-cdn.com/v4/letter/d/3e96dc/32.png) [@Daniel\_Wade](https://forum.inductiveautomation.com/u/Daniel_Wade)\
**Post date:** [April 17, 2023, 6:46pm UTC](https://forum.inductiveautomation.com/t/1st-arg-cant-be-coerced-to-java-util-date/72537/1 "2023-04-17T18:46:43Z")

</div>

I know this is going to be something simple, but I'm running my head into a wall. I'm just trying to get the seconds between a date and now() and I get "1st Arg Can't be Coerced to Java.Util.Date" error in the script console.

Here is the code:  
timeValSec = system.date.secondsBetween("Mon May 04 09:51:52 CDT 2009", system.date.now())

The 1st arg string came directly from a java page, so I'm not understanding what is going on here. Any help would be appreciated. Thanks.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [April 17, 2023, 6:49pm UTC](https://forum.inductiveautomation.com/t/1st-arg-cant-be-coerced-to-java-util-date/72537/2 "2023-04-17T18:49:42Z")

</div>

> [@Daniel\_Wade](#):
>
> The 1st arg string came directly from a java page

There is your answer, you are feeding it a string not a date. Depending on where that date string is coming from, you either need to read the component property that is a date type and not string, or parse the string into a date using [system.date.parse()](https://docs.inductiveautomation.com/display/DOC81/system.date.parse)

---

<div class="post-metadata">

**Author:** ![Daniel\_Wade](https://avatars.discourse-cdn.com/v4/letter/d/3e96dc/32.png) [@Daniel\_Wade](https://forum.inductiveautomation.com/u/Daniel_Wade)\
**Post date:** [April 17, 2023, 7:04pm UTC](https://forum.inductiveautomation.com/t/1st-arg-cant-be-coerced-to-java-util-date/72537/3 "2023-04-17T19:04:49Z")

</div>

Makes since that I need to correct the string to a date, here's what I get using the parse:

code:  
time1 = system.date.parse("Mon Apr 17 09:51:52 UTC 2023","E MMM dd hh:mm:ss z YYYY")  
print(time1)  
time2 = system.date.parse("Mon Apr 17 09:51:52 UTC 2023","E MMM dd")  
print(time2)

results:  
Mon Jan 02 09:51:52 UTC 2023  
Fri Apr 17 00:00:00 UTC 1970

---

<div class="post-metadata">

**Author:** ![Daniel\_Wade](https://avatars.discourse-cdn.com/v4/letter/d/3e96dc/32.png) [@Daniel\_Wade](https://forum.inductiveautomation.com/u/Daniel_Wade)\
**Post date:** [April 17, 2023, 7:10pm UTC](https://forum.inductiveautomation.com/t/1st-arg-cant-be-coerced-to-java-util-date/72537/4 "2023-04-17T19:10:07Z")

</div>

Without the format reference:

code:  
time3 = system.date.parse("Mon Apr 17 09:51:52 UTC 2023")  
print(time3)

results:  
Java Traceback:  
Traceback (most recent call last):  
File "", line 6, in   
at java.base/java.text.DateFormat.parse(Unknown Source)

```
at com.inductiveautomation.ignition.common.script.builtin.DateUtilities.parse(DateUtilities.java:445)

at jdk.internal.reflect.GeneratedMethodAccessor415.invoke(Unknown Source)

at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.base/java.lang.reflect.Method.invoke(Unknown Source)

```

java.text.ParseException: java.text.ParseException: Unparseable date: "Mon Apr 17 09:51:52 UTC 2023"

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [April 17, 2023, 7:11pm UTC](https://forum.inductiveautomation.com/t/1st-arg-cant-be-coerced-to-java-util-date/72537/5 "2023-04-17T19:11:35Z")

</div>

I tried this and it seems to be correct?

```auto
print system.date.parse("Mon Apr 17 09:51:52 UTC 2023","E MMM dd HH:mm:ss Z yyyy")

```

Output  
`Mon Apr 17 05:51:52 EDT 2023`

---

<div class="post-metadata">

**Author:** ![Daniel\_Wade](https://avatars.discourse-cdn.com/v4/letter/d/3e96dc/32.png) [@Daniel\_Wade](https://forum.inductiveautomation.com/u/Daniel_Wade)\
**Post date:** [April 17, 2023, 7:24pm UTC](https://forum.inductiveautomation.com/t/1st-arg-cant-be-coerced-to-java-util-date/72537/6 "2023-04-17T19:24:38Z")

</div>

Yes. The reason yours works is because of the lower case "y" in the formatting string. It was throwing my entire date off. I guess I need to take a break. Thank you for the help.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 17, 2023, 7:30pm UTC](https://forum.inductiveautomation.com/t/1st-arg-cant-be-coerced-to-java-util-date/72537/7 "2023-04-17T19:30:31Z")

</div>

You said the date came from java in the first place. If so, then it was a date object originally, that got converted to string by some display operation. Go back to that date object and use it instead of trying to parse the string back into a date. The latter is extraordinarily fragile and you will later regret it.

---

<div class="post-metadata">

**Author:** ![Daniel\_Wade](https://avatars.discourse-cdn.com/v4/letter/d/3e96dc/32.png) [@Daniel\_Wade](https://forum.inductiveautomation.com/u/Daniel_Wade)\
**Post date:** [April 17, 2023, 7:42pm UTC](https://forum.inductiveautomation.com/t/1st-arg-cant-be-coerced-to-java-util-date/72537/8 "2023-04-17T19:42:32Z")

</div>

No, I copied the string from a Java page trying to get the syntax correctly. The date is actually coming from the alarm journal time stamp.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 17, 2023, 7:45pm UTC](https://forum.inductiveautomation.com/t/1st-arg-cant-be-coerced-to-java-util-date/72537/9 "2023-04-17T19:45:36Z")

</div>

What does "copied from a java page" mean? Use Ignition's API to script the retrieval of an alarm event and print the jython `type()` result from the timestamps it provides.

And, if you wish to bypass the API, go look at the alarm journal database tables' column types.

And I'll repeat: Don't parse strings into dates in your scripts. (Possibly excepting user-typed dates/times, but date input components are better for that, too.)
