How to convert firebase timestamp into date and time
If you are looking to get a Date instance from Timestamp
If you need to get just the Date
object from Timestamp, the Timestamp instance comes with a toDate()
method that returns a Date
instance.
For clarity:
Date javaDate = firebaseTimestampObject.toDate()
Your timestamp 1466769937914
equals to 2016-06-24 12:05:37 UTC
. The problem is that you are multiplying the timestamp
by 1000. But your timestamp
already holds a value in milliseconds not in seconds (this false assumption is most likely the reason you have the multiplication). In result you get 1466769937914000
which converted equals to 48450-02-01 21:51:54 UTC
. So technically speaking all works fine and results you are getting are correct. All you need to fix is your input data and the solution is quite simple - just remove the multiplication:
SimpleDateFormat sfd = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
sfd.format(new Date(timestamp));
According to Firebase documentation, the types that are available JSON are:
String
Long
Double
Boolean
Map<String, Object>
List<Object>
Quoting another Stack Overflow post, I suggest you use JSON date string format yyyy-MM-dd'T'HH:mm:ss.SSSZ
instead of epoch timestamp.
Comparing 1335205543511
to 2012-04-23T18:25:43.511Z
, you can noticed that:
- It's human readable but also succinct
- It sorts correctly
- It includes fractional seconds, which can help re-establish chronology
- It conforms to ISO 8601
ISO 8601 has been well-established internationally for more than a decade and is endorsed by W3C, RFC3339, and XKCD