1
votes

I have a Rest Service which receives a JSON object. This JSON has a timestamp value in it. When I convert the JSON object to Java class I use a long variable (which I later use with new Date(long) to get a proper date object) for the timestamp. Everything was working fine as the clients (in Java and JS) were sending timestamp as long.

Now we are adding a Python client to the service, and I am trying to send timestamp as long from Python. But time.time() method in Python always gives a floating point number. So I decided to use a double variable in my Rest Service's Java class and I am getting the timestamp correctly. But then how do I convert this to a Date Object. Any ideas?

1

1 Answers

4
votes

So looking at the documentation (never used Python before) it looks like the time.time() method returns seconds since the epoch, while the Java Date object wants milliseconds from the epoch in its constructor.

So you need to do some fancy math to get the double into a long. From the little bit of Python docs I just read, it seems like it would be easier to convert the seconds to milliseconds in the Python code and send the result as a long to the rest service. Depending on how exact you have to be, you can just multiply the time.time() result by 1000 to get to milliseconds then round the rest to drop the floating point piece.