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?
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.
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