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

前端 未结 4 1968
北海茫月
北海茫月 2021-01-18 07:04

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

from the Firestore doc

  Map up         


        
相关标签:
4条回答
  • 2021-01-18 07:28

    You need to change this line of code:

    private long timestamp;
    

    with

    private Map<String, String> timestamp;
    

    And here are the corresponding public setters and getters:

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

    Remember, that you are setting the timestamp as Map and when you are retrieving, you are retrieving it as a Long.

    0 讨论(0)
  • 2021-01-18 07:35
    private com.google.firebase.Timestamp date;
    
    public yourModel(com.google.firebase.Timestamp date) {
            this.date= date;
    }
    
    public com.google.firebase.Timestamp getDate() {
            return date;
    }
    
    public void setDate(com.google.firebase.Timestamp date) {
            this.date = date;
    }
    
    0 讨论(0)
  • 2021-01-18 07:41

    If you only want a timestamp and not an accurate time event then you can use

    System.currentTimeMillis();
    
    0 讨论(0)
  • 2021-01-18 07:50

    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

    0 讨论(0)
提交回复
热议问题