Voy Voy - 1 year ago 411
Android Question

How to use the Firebase server timestamp to generate date created?

Currently, the Google's version of

which is used as a directive for Firebase to fill that field with the server timestamp once you save the data to the Firebase server.

When you create your data on the client side however, you don't have the actual timestamp to play with yet (ie. use as the creation date). You only will have an access to the timestamp after the initial save and consequent retrieval, which - I imagine - is sometimes too late and not very elegant.

Before Google:

Update: Ignore this section as it is incorrect - I misunderstood the examples.
always returned the

As far as I understand in pre-google Firebase there seemed to be a server-generated timestamp available that allowed you to acquire the actual timestamp:

import com.firebase.client.ServerValue;
ServerValue.TIMESTAMP // eg. 1466094046

(ref 1, ref 2)


  1. Is such save/retrieval the only way to get the server-generated creation date on my model instances?

  2. If yes can you propose a method of implementing such pattern?

  3. Am I understanding correctly ServerValue.TIMESTAMP has changed with Google's acquisition of Firebase? Update: No, @FrankvanPuffelen replied that nothing's changed during acquisition.


I'm not considering using
new Date()
on client side as I've been reading it's not safe, though please share your thoughts if you think different.


When you use the ServerValue.TIMESTAMP constant in a write operation, you're saying that the Firebase Database server should determine the correct timestamp when it executes the write operation.

Let's say we run this code:

ref.addValueEventListener(new ValueEventListener() {
    public void onDataChange(DataSnapshot dataSnapshot) {

    public void onCancelled(DatabaseError databaseError) { }

This will execute as follows:

  1. you attach a listener
  2. you write a value with ServerValue.TIMESTAMP
  3. the Firebase client immediate fires a value event with an approximation of the timestamp it will write on the server
  4. your code prints that value
  5. the write operation gets sent to the Firebase servers
  6. the Firebase servers determine the actual timestamp and write the value to the database (assuming no security rules fail)
  7. the Firebase server send the actual timestamp back to the client
  8. the Firebase client raises a value event for the actual value
  9. your code prints that value

Nothing changed in the way we generate the `ServerValue.TIMESTAMP since Firebase joined Google. Code that worked before, will continue to work. That also means that the first answer you linked is a valid way to handle it.