Converting a string into a java.util.Date

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:

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

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]

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!

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.

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

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.

1 Like

You can just system.date functions now.

1 Like

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. :slight_smile:

Your script can now be shortened to something like:

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

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