views:

1172

answers:

1

I can create a similar Date object in Java by java.util.Date(milliseconds). How do I create the comparable in Python?

+5  A: 

Just convert it to timestamp

datetime.datetime.fromtimestamp(ms//1000)
vartec
A note -- in Python 3, (/) will perform floating-point division. To perform integral division, use (//).
John Millikin
Don't you actually want float division anyway? Otherwise you're losing any precision below 1 second (held in the fractional part of the timestamp). Better to use ms/1000.0 with no truncation.
Brian
Not all platforms support fractional timestamps -- better to stick with POSIX's integers.
John Millikin
Surely datetime always supports it though? If we've got sub-second precision, it seems wrong to throw it away. If we don't there's no harm done - we just retain that original precision.
Brian