I want to use the value of the long iam timestamp for ServerValue.Timestamp… here is a solution to the problem.
I want to use the value of the long iam timestamp for ServerValue.Timestamp
I’m using ServerValue.TimeStamp and uploading to firebase using a hashmap
HashMap<String, Object> map = new HashMap();
map.put("timestamp", ServerValue.TIMESTAMP);
But I need to get the value of TimeStamp without uploading it to firebase
I tried to convert it to long but always got ClassCastException
Map<String, Object> currentTime = new HashMap<>();
currentTime.put("timestamp", ServerValue.TIMESTAMP);
return (long) currentTime.get("timestamp");
Does it help?!
Solution
It works for me when I make small changes to the model class by substitution
Map<String , String> timestamp;
with
Object timestamp;
Because the Object class is the parent of all classes in java, any subclass will work fine because when you save the value in firebase, it’s fine, and when retrieving the value, you just need to convert this object to Long
This is the model I used
public class UserModel {
private String name , phone, email, birthDate, userType, address;
Object timestamp;
for firebase retrieving data
public UserModel() {
}
public UserModel(String name, String phone, String email, String birthDate, String userType, String address) {
this.name = name;
this.phone = phone;
this.email = email;
this.birthDate = birthDate;
this.userType = userType;
this.address = address;
get Date from firebase server automatic
this.timestamp = ServerValue.TIMESTAMP;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
public String getEmail() {
return email;
}
public String getBirthDate() {
return birthDate;
}
public String getUserType() {
return userType;
}
public String getAddress() {
return address;
}
@Override
public String toString() {
String data = " user name" + this.name+" , user Address : "
+this.address+" , user phone "+this.phone+" , user email : "+this.email
+" , user birthdate : "+this.birthDate+" , user type : "+this.userType
+ " , registered time "+this.timestamp;
return data;
}
/* public String getLoginDate() {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.mmm'Z'");
Date date = new Date(si)
return ;
}*/
}
and i uploaded it in firebase successufly
and here is the reslt i recieved when download data from firebase