2

I am working on a chat app and I want to store timestamp of my messages. My Data Class for Messages is:

import com.google.firebase.firestore.ServerTimestamp;
import java.util.Date;

public class messgaesDataClass {
    private String messageText;
    private int messageStatus;
    private String messagefrom;
    private @ServerTimestamp Date timestamp;

    public messgaesDataClass(String messageText, int messageStatus, String messagefrom, Date time) {
        this.messageText = messageText;
        this.messageStatus = messageStatus;
        this.messagefrom = messagefrom;
        this.time = time;
    }

    public messgaesDataClass() {
    }

    public String getMessageText() {
        return messageText;
    }

    public void setMessageText(String messageText) {
        this.messageText = messageText;
    }

    public int getMessageStatus() {
        return messageStatus;
    }

    public void setMessageStatus(int messageStatus) {
        this.messageStatus = messageStatus;
    }

    public String getMessagefrom() {
        return messagefrom;
    }

    public void setMessagefrom(String messagefrom) {
        this.messagefrom = messagefrom;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }
}

I don't understand how to make object of this class. What can I pass in the constructor of this class to initialize timestamp attribute?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
IO Devs
  • 383
  • 1
  • 4
  • 12

1 Answers1

5

To solve this, you should remove the Date time as the argument of your constructor. Your constructor should be like this:

public messgaesDataClass(String messageText, int messageStatus, String messagefrom) {
    this.messageText = messageText;
    this.messageStatus = messageStatus;
    this.messagefrom = messagefrom;
}

There is no need to initialize the time object in your constructor. Firebase servers will read your time field as it is a ServerTimestamp (because of the annotation), and it will populate that filed with the server timestamp accordingly.

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • check this link .. https://stackoverflow.com/questions/49111882/how-to-save-previous-timestamp-to-firebase-firestore – Amit Mishra Jan 16 '19 at 12:56
  • 2
    @AmitMishra The OP is looking for a solution in Android while that answer is for Javascript. – Alex Mamo Jan 16 '19 at 12:57
  • @AlexMamo That answer works for Android as well - just need to use `com.google.firebase.firestore.FieldValue.serverTimestamp()` – Pavlo28 May 27 '19 at 09:45
  • What if we have more such timestamp fields, for example, for capturing last login time, last active time etc., with the same `@ServerTimestamp` annotation. How would we update these? – Shahood ul Hassan Sep 27 '19 at 15:04
  • @ShahoodulHassan That sounds as a question regarding multiple fields. In this case please post another fresh question using its own [MCVE](https://stackoverflow.com/help/mcve), so me and other Firebase developers can help you. – Alex Mamo Sep 29 '19 at 10:36
  • @AlexMamo so you say not to provide Date in the constructor, but what if we want to use Date in the constructor in this way: ```new Date("1 hour ago")```. How to pass this to Firestore? – H.Karatsanov Jul 02 '20 at 21:45
  • 1
    @H.Karatsanov In that case, you are passing a custom Date, a situation in which Firestore will not generate the serverTimestamp for you. – Alex Mamo Jul 03 '20 at 08:52