3

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 ?!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
fady zarif
  • 79
  • 1
  • 11
  • Looks the same as https://stackoverflow.com/questions/36658833/firebase-servervalue-timestamp-in-java-data-models-objects – Scary Wombat Jul 20 '17 at 00:58
  • yes I tried this but the same problem ClassCastException @ScaryWombat – fady zarif Jul 20 '17 at 01:43
  • There is no way to get the long value from the Firebase server, without first sending the string value to that server. See [Osama's original answer](https://stackoverflow.com/questions/25744398/retrieve-servervalue-timestamp-from-firebase-in-android-app-when-data-is-sent) or [Lyla's more advanced answer](https://stackoverflow.com/questions/33096128/when-making-a-pojo-in-firebase-can-you-use-servervalue-timestamp). – Frank van Puffelen Jul 20 '17 at 02:27

2 Answers2

2

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

ahmed khattab
  • 2,311
  • 3
  • 16
  • 30
0

I had same problem and i solved it.hope this will help you :D

    String key = mDatabase.push().getKey();mDatabase.child(ke).setValue(mMap).addOnCompleteListener(new OnCompleteListener<Void>() {
                                    @Override
                                    public void onComplete(@NonNull Task<Void> task) {
                                        mDatabase.push().child("date").setValue(ServerValue.TIMESTAMP);
                                    }
                                });
mDatabase.child(ke).child("date").setValue(ServerValue.TIMESTAMP);
Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
Irakli
  • 58
  • 2
  • 9