I have a application that uses a formatted text field for users to input dates, and that part is a must have so I cannot change it.
I want to be able to check if the date is a valid date (ie no 2020-02-39) and if it is, pass it on to the next part of my application as a java.util.Date object, and if not raise an exception.
I have this
from java.text import SimpleDateFormat
...
inputFormat = SimpleDateFormat('yyyy-MM-dd')
dt = data['date']
dateOut = SimpleDateFormat(inputFormat, dt)
But this does not seem to be working as expected. The formatted input box is yyyy-MM-dd and will have two values for the day always. Any insight as to whats going wrong?
from datetime import datetime
def checkDate(date_text):
try:
if date_text != datetime.strptime(date_text, "%Y-%d-%m").strftime('%Y-%d-%m'):
raise ValueError
return True
except ValueError:
return False
print checkDate(event.source.parent.getComponent('Text Field').text)
Is there a compelling reason to not use system.date.parse
for this?
1 Like
Nope that was it. I had tried the datetime method but that left me with a python date object when I needed a java date object. I had forgotten about system.date but its good to know it works with java date objects.
This does just coerce things like 2020-02-47 to 2020-03-18 which isn’t great for my needs but at least I have the right object type now. I can code that error checking manually if need be.a