I want to convert entered timestamp into Firebase server timestamp Android
For example selected timestamp is -> 1631724053. Note this timestamp is got from the selected date.
Want to convert timestamp(firebase server timestamp) is -> January 01, 2021 at 3:37:59 PM UTC 5:30
Thanks in advance
CodePudding user response:
You didn't specify whether you're using Java or Kotlin, however you can create a Firestore Timestamp from an Android date
When calling the Timestamp constructor you would pass in your date object,so make sure you're using valid Dates as outlined in the Android documentation.
CodePudding user response:
An object of type Firestore Timestamp represents:
A Timestamp represents a point in time independent of any time zone or calendar, represented as seconds and fractions of seconds at nanosecond resolution in UTC Epoch time.
As also @Hydra already mentioned in her comment, you can convert a long value as:
1631724053
Which actually represents:
Wednesday, September 15, 2021 4:40:53 PM
Into a Firestore object bypass the time epoch to the constructor. However, a more appropriate way of handling this kind of situation is to use FieldValue.serverTimestamp(), which is actually a token that gets sent to Firebase Firestore servers, where the actual Timestamp is determined and written into the database.