# Turning a string into a java.util.Date, and catching exceptions?

**URL:** <https://forum.inductiveautomation.com/t/turning-a-string-into-a-java-util-date-and-catching-exceptions/31872>\
**Category:** Ignition\
**Tags:** ignition80\
**Created:** [February 3, 2020, 4:32pm UTC](https://forum.inductiveautomation.com/t/turning-a-string-into-a-java-util-date-and-catching-exceptions/31872 "2020-02-03T16:32:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [February 3, 2020, 4:32pm UTC](https://forum.inductiveautomation.com/t/turning-a-string-into-a-java-util-date-and-catching-exceptions/31872/1 "2020-02-03T16:32:36Z")

</div>

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

```auto
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?

---

<div class="post-metadata">

**Author:** ![MMaynard](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mmaynard/32/3366_2.png) [@MMaynard](https://forum.inductiveautomation.com/u/MMaynard)\
**Post date:** [February 3, 2020, 4:39pm UTC](https://forum.inductiveautomation.com/t/turning-a-string-into-a-java-util-date-and-catching-exceptions/31872/2 "2020-02-03T16:39:16Z")

</div>

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [February 3, 2020, 4:44pm UTC](https://forum.inductiveautomation.com/t/turning-a-string-into-a-java-util-date-and-catching-exceptions/31872/3 "2020-02-03T16:44:47Z")

</div>

Is there a compelling reason to not use [`system.date.parse`](https://docs.inductiveautomation.com/display/DOC80/system.date.parse) for this?

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [February 3, 2020, 4:49pm UTC](https://forum.inductiveautomation.com/t/turning-a-string-into-a-java-util-date-and-catching-exceptions/31872/4 "2020-02-03T16:49:54Z")

</div>

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
