# Convert Java.Util.Date to datetime.datetime

**URL:** <https://forum.inductiveautomation.com/t/convert-java-util-date-to-datetime-datetime/9910>\
**Category:** General Discussion\
**Created:** [April 14, 2015, 6:02pm UTC](https://forum.inductiveautomation.com/t/convert-java-util-date-to-datetime-datetime/9910 "2015-04-14T18:02:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Howell](https://avatars.discourse-cdn.com/v4/letter/h/dfb087/32.png) [@Howell](https://forum.inductiveautomation.com/u/Howell)\
**Post date:** [April 14, 2015, 6:02pm UTC](https://forum.inductiveautomation.com/t/convert-java-util-date-to-datetime-datetime/9910/1 "2015-04-14T18:02:36Z")

</div>

After querying a SQL database…  
resultData = system.db.runPrepQuery(“SELECT StartTime FROM…”)

I use this which sets startTime as a java.util.date type…  
startTime = shiftData[0][“StartTime”]

But I’m needing it to be a datetime.datetime. Is it possible to either access it from the pydataset as a datetime.datetime or to convert the java.util.date to datetime.datetime?

Thanks

---

<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:** [April 14, 2015, 7:24pm UTC](https://forum.inductiveautomation.com/t/convert-java-util-date-to-datetime-datetime/9910/2 "2015-04-14T19:24:17Z")

</div>

It’ll be something like adding this:

```auto
from time import gmtime

convertedTime=gmtime(float(startTime.getTime())/1000)
```

Now for the explanation:

getTime() returns the number of milliseconds since epoch (it’s how Java looks at time).

gmtime() is the Pythonic way of converting seconds (not milliseconds) since epoch and converts it to a datetime structure. Another cool thing is if you use gmtime() without giving a value, it gives the current time. ![:thumb_right:](https://us1.discourse-cdn.com/inductiveautomation/original/1X/2026e28c0c425034352c1b75807edf956adc75f4.gif ":thumb\_right:")

The last bit to know is that gmtime() requires the argument to be a float and again, be in seconds, not milliseconds. Hence the float conversion and dividing by 1000

Hope this helps!
