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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With