Friday, May 29, 2020

Convert Cloud Firestore Timestamp to readable date

Convert Cloud Firestore Timestamp to readable date:
When you read a timestamp type field out of a document in Cloud Firestore, it will arrive as a Timestamp type object in Java. Be sure to read the linked Javadoc to find out more about it.
Timestamp timestamp = (Timestamp) documentSnapshot.getData().get("last_login_date");
The Timestamp has two components, seconds and nanoseconds. If those values are not useful to you, you can convert the Timestamp to a Java Date object using its toDate() method, but you might lose some of the timestamp's nanosecond precision since Date objects only use microsecond precision.
Date date = timestamp.toDate();
With a Date object, you should be able to easily use other date formatting tools, such as Android's own date formatting options. You can also use the Date's toMillis() method to compare it with the current time from System.currentTimeMillis();

No comments:

Post a Comment