0

Tried to convert firestore timestamp to string and I got this!

enter image description here

I tried String date = FieldValue.serverTimestamp().toString(); and instead of time stamp I got this as shown in Screenshot1

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Ketan Choyal
  • 93
  • 2
  • 7
  • The serverTimestamp is a server value so you would first have to save it and then read it back before you can convert it. – André Kool Mar 05 '18 at 15:55
  • is there any way to directly access the serverTimestamp value without storing it into server? @AndréKool – Ketan Choyal Mar 05 '18 at 16:03
  • 1
    As far as i know that is not possible. FieldValue.serverTimestamp() is a placeholder, its actually saying "I want firebase to replace this with the server timestamp". – André Kool Mar 05 '18 at 16:08

2 Answers2

1

The date inside a Firestore database must be stored as a Date object as explained here. Assuming that you have public getter named getDate() inside your model class, to print the date, please use the following code:

Date date = yourModelClass.getDate();
if (date != null) {
    DateFormat dateFormat = SimpleDateFormat.getDateInstance(DateFormat.MEDIUM, Locale.US);
    String creationDate = dateFormat.format(date);
    Log.d("TAG", creationDate);
}

What you are actually printing there is the address of the FieldValue class from the memory.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
-1

okay so the toString() will not output the date formatted since the object returned by the statement FieldValue.serverTimestamp() is a Date object so you can do something like this to handle date formatting.

Date now = FieldValue.serverTimestamp();
SimpleDateFormat dateFormatter = new SimpleDateFormat("E, y-M-d 'at' h:m:s a z");
Log.i("Format 1:   ", dateFormatter.format(now));
Ali Kaissi
  • 44
  • 8
  • well the previous answer did not contain the relation to serverTimesTamp however i have edited my answer thank you for mentioning that – Ali Kaissi Mar 05 '18 at 16:18