# Converting a string into a java.util.Date

**URL:** <https://forum.inductiveautomation.com/t/converting-a-string-into-a-java-util-date/8941>\
**Category:** Ignition\
**Created:** [August 21, 2014, 8:58pm UTC](https://forum.inductiveautomation.com/t/converting-a-string-into-a-java-util-date/8941 "2014-08-21T20:58:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![drewdin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/drewdin/32/3363_2.png) [@drewdin](https://forum.inductiveautomation.com/u/drewdin)\
**Post date:** [August 21, 2014, 8:58pm UTC](https://forum.inductiveautomation.com/t/converting-a-string-into-a-java-util-date/8941/1 "2014-08-21T20:58:24Z")

</div>

I have a string i created with python to input to a Popup Calender.

When I try to link it to the date, I get the Error:

```auto
can't convert 'Thu Aug 21 16:56:40 EDT 2014' to java.util.Date
```

How do i convert a string into a java.util.Date?

Thanks all

---

<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 22, 2014, 9:51am UTC](https://forum.inductiveautomation.com/t/converting-a-string-into-a-java-util-date/8941/2 "2014-08-22T09:51:16Z")

</div>

Hi drewdin.

Can you post a sample of code giving your problem?

EDIT: The way that has always worked for me is to use java.text.SimpleDateFormat

[code]from java.text import SimpleDateFormat

inputFormat=SimpleDateFormat(“EEE MMM dd HH:mm:ss Z yyyy”) # Must match the format of the input string  
dt=“Wed Mar 26 10:13:54 CET 2014”  
event.source.parent.getComponent(‘Popup Calendar’).date=dateOut

inputFormat=SimpleDateFormat(“yyyy/MM/dd HH:mm:ss”) # Must match the format of the input string  
dt=“1967/09/24 15:25:45”  
dateOut=SimpleDateFormat.parse(inputFormat,dt)  
event.source.parent.getComponent(‘Popup Calendar’).date=dateOut[/code]

---

<div class="post-metadata">

**Author:** ![drewdin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/drewdin/32/3363_2.png) [@drewdin](https://forum.inductiveautomation.com/u/drewdin)\
**Post date:** [August 22, 2014, 3:11pm UTC](https://forum.inductiveautomation.com/t/converting-a-string-into-a-java-util-date/8941/3 "2014-08-22T15:11:02Z")

</div>

Thanks Jordan,

I ended up using:

[code]from java.text import SimpleDateFormat

```
		fmt = '%a %b %d %H:%M:%S %Z %Y'
		endtimestr = time.strftime(fmt)

		inputFormat=SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy") # Must match the format of the input string
		endtime=SimpleDateFormat.parse(inputFormat,endtimestr)[/code]

```

Where can i learn whats available for java in jython? Thanks again!

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [August 22, 2014, 4:46pm UTC](https://forum.inductiveautomation.com/t/converting-a-string-into-a-java-util-date/8941/4 "2014-08-22T16:46:41Z")

</div>

Everything in Java is available to Jython. It is one of the awesome things about Jython.

You could buy a book on Java or use Java resources on the Internet.

---

<div class="post-metadata">

**Author:** ![shibubalaji](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/shibubalaji/32/9165_2.png) [@shibubalaji](https://forum.inductiveautomation.com/u/shibubalaji)\
**Post date:** [December 8, 2018, 3:40pm UTC](https://forum.inductiveautomation.com/t/converting-a-string-into-a-java-util-date/8941/5 "2018-12-08T15:40:50Z")

</div>

> [@JordanCClark](#):
>
> SimpleDateFormat

Was this issue sorted out. I have the same issue...

can't convert '12/08/2018 09:34:40 AM' to java.util.Date

Here is my code in event handler...Code is executing till last line, because messagebox appears for newdate, but it is not executing the last line and error is can't convert to java.uitl.Date....  
Any solutions for this...

import time  
import datetime  
from java.util import Calendar  
now = datetime.datetime.now()  
dateSelector = event.source.parent.getComponent('PopupCalendarEnd')  
if dateSelector.date \> datetime.datetime.now():  
system.gui.messageBox (str(now))  
newdate=now.strftime("%m/%d/%Y %H:%M:%S %p")  
system.gui.messageBox (newdate)  
dateSelector.date=newdate

---

<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:** [December 8, 2018, 7:20pm UTC](https://forum.inductiveautomation.com/t/converting-a-string-into-a-java-util-date/8941/6 "2018-12-08T19:20:37Z")

</div>

You are using python’s (jython’s) datetime module. It doesn’t yield a java.util.Date. Use the java datatypes as shown in Jordan’s post.

---

<div class="post-metadata">

**Author:** ![drewdin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/drewdin/32/3363_2.png) [@drewdin](https://forum.inductiveautomation.com/u/drewdin)\
**Post date:** [December 8, 2018, 11:32pm UTC](https://forum.inductiveautomation.com/t/converting-a-string-into-a-java-util-date/8941/7 "2018-12-08T23:32:58Z")

</div>

You can just system.date functions now.

---

<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:** [December 10, 2018, 2:24pm UTC](https://forum.inductiveautomation.com/t/converting-a-string-into-a-java-util-date/8941/8 "2018-12-10T14:24:45Z")

</div>

As has been mentioned (thank you, gentlemen!), you are not using Java datatypes. That being said, Starting with v7.8.something, there are a whole slew of functions added under system.date.\* that use java data types. 🙂

Your script can now be shortened to something like:

```python
dateSelector = event.source.parent.getComponent('Popup Calendar')
now = system.date.now()

if system.date.isAfter(dateSelector.date , now):
  dateSelector.date = now

```
