I have extracted accerometer data from a android wearable. While looking at the data i realised the timestamp is not unix covertable. After research i saw the timestamp was actually nanoseconds in uptime. My question is the same as Accelerometer SensorEvent timestamp. However due to me not knowing Java i dont know how convert it using the solutions provided. Is there any python ways i can convert the nanoseconds in uptime into a readable date time format? An example of the timestamp would be "45900482044637".
3 Answers
tl;dr
Duration.ofNanos ( 45_900_482_044_637L )
PT12H45M0.482044637S
java.time
Java 8 and later has a Duration
class for this purpose, as part of the new java.time framework (see Tutorial). These new classes have nanosecond resolution (nine decimal places in fractional second). A Duration
represents a span of time as a number of hours, minutes, and seconds.
Android currently does not use Java 8 technology, but there is a back-port of java.time to Java 6 & 7. Further adapted to Android in the ThreeTenABP project.
Note the L
appended to numeric literal for a long
. Also, underscores make lengthy numbers easier for humans to decipher.
long input = 45_900_482_044_637L;
Let's convert that number to a Duration
object.
Duration duration = Duration.ofNanos ( input );
When we generate a String representation of that Duration
object, we get a String formatted using the ISO 8601 standard. That standard uses the pattern PnYnMnDTnHnMnS
where the P
marks the beginning and the T
separates years-months-days from the hours-minutes-seconds.
System.out.println ( "duration: " + duration );
The answer is twelve hours, forty-five minutes, and a fraction of a second.
duration: PT12H45M0.482044637S
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.

- 303,325
- 100
- 852
- 1,154
To get the uptime in human-readable format in Python:
>>> from datetime import timedelta
>>> ns = 45900482044637
>>> print(timedelta(microseconds=round(ns, -3) // 1000))
12:45:00.482045

- 399,953
- 195
- 994
- 1,670
Use:
long millis = TimeUnit.MILLISECONDS.convert(nanosecond, TimeUnit.NANOSECONDS);
Date date = new Date(millis );
DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd hh:mm aaa");
String dateFormatted = formatter.format(date);

- 4,470
- 5
- 28
- 50
-
I don't think OP wants January 1, 1970 date. – jfs Nov 25 '15 at 09:02