Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use FieldValue.serverTimestamp() to custom model class in android

I am using Firestore as a database and now i want to store server timestamp when user register.

from the Firestore doc

  Map<String,Object> updates = new HashMap<>();
  updates.put("timestamp", FieldValue.serverTimestamp());
  docRef.update(updates)
  .addOnCompleteListener(new OnCompleteListener<Void>() {  //-- }

But now i want this time stamp in my model class.

public class UserModel implements Serializable {
    private String name;
    private String uid;
    private String email;
    private String password;
    private long timestamp;

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

how can i achieve this. Please help me.

like image 255
Pradeep Kumar Avatar asked Oct 31 '17 12:10

Pradeep Kumar


1 Answers

After lots of research i found a solution for this.

 @ServerTimestamp
 private Date timestamp;

 public Date getTimestamp() {
     return timestamp;
 }
 public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
 }

and when calling this class

UserModel userModel = new UserModel();
userModel.setEmail(email);
userModel.setPassword(password);
userModel.setName(name);
userModel.setUid(uid);

insertUserDataToFireStore(userModel); 

This is the link where i found the solution Click here

like image 140
Pradeep Kumar Avatar answered Nov 11 '22 21:11

Pradeep Kumar