Retrieve ServerValue.Timestamp from Firebase in Android app, when data is sent

24,572

Solution 1

Firebase.ServerValue.TIMESTAMP is set as a Map (containing {.sv: "timestamp"}) which tells Firebase to populate that field with the server's time. When that data is read back, it is the actual unix time stamp which is a Long.

Something like this will work:

Firebase ref = new Firebase("https://YOUR-FIREBASE.firebaseio.com");    

ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        Long timestamp = (Long) snapshot.getValue();
        System.out.println(timestamp);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

ref.setValue(ServerValue.TIMESTAMP);

For another example, you can see my answer to this question: Android chat crashes on DataSnapshot.getValue() for timestamp

Solution 2

If you don't want to write to database to figure out the timestamp, you can get instant approximation of it. Works even offline and it's good enough for most cases.

Read value

/.info/serverTimeOffset

And add it to new Date().time()

Solution 3

I know this question already been answered, but I wanted to share my version of the solution.

When I use the server's timestamp, i usually need to use it more than once, i.e I have some kind of startTime and endTime which both depend on the server's time, where startTime is NOW and endTime is X seconds / minutes / hours after startTime, so to spare the multiple requests to the server I save the server's time in a root child called serverTime in the database, and I use it to set all dependent values.

Another thing, because of the way Firebase works with the ServerValue.Timestamp, eventually it fires 2 events (added and changed), the first one with local timestamp and the second with the actual server's timestamp. So to overcome the issue of not receiving the correct time I added a simple OnCompleteListener.

A short example code:

import android.support.annotation.NonNull;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.ServerValue;
import com.google.firebase.database.ValueEventListener;

public class ServerTime {

    public interface OnTimeRetrievedListener {

        void onTimeRetrieved(Long timestamp);
    }

    private final DatabaseReference db;

    public ServerTime(DatabaseReference db) {
        this.db = db.child("serverTime");
    }

    /**
     * Gets the server's timestamp in milliseconds.
     * @param listener {@link OnTimeRetrievedListener}
     */
    public void getTime(final OnTimeRetrievedListener listener) {
        if (listener == null) {
            return;
        }

        db.setValue(ServerValue.TIMESTAMP).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {

                db.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        listener.onTimeRetrieved(dataSnapshot.getValue(Long.class));
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) { }
                });

            }
        });
    }
}

Solution 4

First, send an object to the firebase server

FirebaseDatabase.getInstance().getReference("serverTimeTest").setValue(new KKModel());

Model class

class KKModel{
    public String someField = "value";
    public Object creationDate = ServerValue.TIMESTAMP;

    public String creationDate() {
        return SimpleDateFormat.getDateInstance(DateFormat.SHORT, Locale.US).format(creationDate);
    }
}

Usage

object.creationDate() working fine test results "9/6/18"

Share:
24,572
user3537089
Author by

user3537089

Updated on July 09, 2022

Comments

  • user3537089
    user3537089 almost 2 years

    I would like to know how to use Firebase's ServerValue.TIMESTAMP method, when I want to create a timestamp at the Firebase server, and then retrieve it to the local client.

    In Firebase guides, only javascript has a more detailed description of this case, but I'm having a hard time figureing how to translate this in to my Android appliction.

    Thanks in advance!