I want the value of timestamp in long format iam using ServerValue.Timestamp

Viewed 3107

Iam using ServerValue.TimeStamp and upload to firebase using hashmap HashMap<String, Object> map = new HashMap(); map.put("timestamp", ServerValue.TIMESTAMP);

but I need to get the value of TimeStamp in long without upload it to the firebase I have tried to cast it to long but always receive ClassCastException

        Map<String, Object> currentTime = new HashMap<>();
    currentTime.put("timestamp", ServerValue.TIMESTAMP);
    return (long) currentTime.get("timestamp");  

any help ?!

2 Answers

it worked for me when i do small change in model class by replacing

Map<String , String> timestamp;

with

Object timestamp;

as Object class is the parent of all classes in java so any subclass will work fine with it as when you save the value in firebase it's ok and when retrieve value all just you need is to cast this object to Long

here's my 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

firebase image

and here is the reslt i recieved when download data from firebase

data after download from firebase

Related