Hhis may be asked many times but none of the solutions didn't works for me now let me tell you what I have tried so far am using just simple model class to save values in Cloud Firestore db and in that model class am having using timestamp field but while inserting my timestamp is always null don't know where am making mistake now let me post my code :
import com.google.firebase.firestore.ServerTimestamp;
import java.util.Date;
public class DriverMasterModel {
private String test;
@ServerTimestamp
private Date date;
public DriverMasterModel() {}
public DriverMasterModel(String test) { this.test = test; }
public String getTest() { return test; }
@PropertyName("Created_At")
public Date getDate() { return date; }
}
and here am inserting the data to db:
DriverMasterModel driverMasterModel=new DriverMasterModel();
driverMasterModel.setTest("test");
FirebaseFirestore dbb=FirebaseFirestore.getInstance();
dbb.collection("Test").add(driverMasterModel).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
}
});
But my Created_at field always null in Firestore db.
You are getting always
null
because your model class is wrong. YourMasterModel
class should look like this:See, the name of the class is the same as the name of the constructor and the name of the
Date
object is simpledate
and notCreated_At
.Edit: According to your comment, please see the new configuration of your model class: