Skip to content

Instantly share code, notes, and snippets.

@huskercane
Created July 29, 2014 15:08
Show Gist options
  • Save huskercane/b9ca940f3735b9d30e3b to your computer and use it in GitHub Desktop.
Save huskercane/b9ca940f3735b9d30e3b to your computer and use it in GitHub Desktop.
Convert from java timestamp to python datetime and vice versa
def _convert_java_millis(java_time_millis):
"""Provided a java timestamp convert it into python date time object"""
ds = datetime.datetime.fromtimestamp(
int(str(java_time_millis)[:10])) if java_time_millis else None
ds = ds.replace(hour=ds.hour,minute=ds.minute,second=ds.second,microsecond=int(str(java_time_millis)[10:]) * 1000)
return ds
def _convert_datetime_java_millis(st):
"""Provided a python datetime object convert it into java millis"""
return long(time.mktime(st.timetuple()) * 1e3 + st.microsecond/1e3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment