How to save the current date/time when I add new value to Firebase Realtime Database

I want to save the current date/time in specific field when I add new value to Firebase Realtime Database via control panel.

How can I achieve that?

Please help me.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

The best practice is to save your data as a TIMESTAMP like this ServerValue.TIMESTAMP.

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
Map map = new HashMap();
map.put("timestamp", ServerValue.TIMESTAMP);

Also remember, when you set the TIMESTAMP, you set it as a Map, but when you retrieve it, you retrieve it as a Long. To get the data back, i suggest you use this method:

public static String getTimeDate(long timestamp){
        DateFormat dateFormat = getDateTimeInstance();
        Date netDate = (new Date(timestamp));
        return dateFormat.format(netDate);
    } catch(Exception e) {
        return "date";

Edit: The model class should look like this:

public class YourModelClass {
    //private fields
    private Map<String, String> timestamp;

    public YourModelClass() {}

    //public setters and getters for the fields

    public void setTimestamp(Map<String, String> timeStamp) {this.timestamp= timestamp;}
    public Map<String, String> getTimestamp() {return timestamp;}

Remember, ServerValue.TIMESTAMP is just a token that Firebase Realtime Database converts to a number on server side when it’s used as a child value during write operation. The date only appears in the database after the write operation completes.

To get the timestamp, there is also another approach, which would be to write a frunction in Cloud Functions for Firebase and it will be as easy as:

exports.currentTime = functions.https.onRequest((req, res) => {
    res.send({"timestamp":new Date().getTime()})

You can host this in Cloud Function and get the server timestamp without user interaction.

Solution 2

Alex Mamo is right but then this is how you should pass incase you call it from a model class

public class FirebMessage {
    public String message;
    public String senderPhoneNumber;
    public String receiverPhoneNumber;
    public Map time;

    public FirebMessage() {

    public FirebMessage(String message, String senderPhoneNumber, String receiverPhoneNumber, Map time) {
        this.message = message;
        this.senderPhoneNumber = senderPhoneNumber;
        this.receiverPhoneNumber = receiverPhoneNumber;
        this.time = time;

Then consume as follows

 private void writeNewMessage(String message, String receiver,String sender) {

        FirebMessage firebMessage = new FirebMessage(message, receiver,sender,ServerValue.TIMESTAMP);
        mDatabase.child("messages").push().setValue(firebMessage, new DatabaseReference.CompletionListener() {
            public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                if (databaseError != null) {
                   Log.e(TAG,"Data Not saved");

                } else {
                    Log.e(TAG,"Data saved successfully");

Solution 3

Use data type Number into Firebase.

  1. Store current millis while storing data into Fireabase.
  2. While retrieving data you will get millis which you were stored previously.
    Parse in into Java Date Instance and work further.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply