-2

I am trying to get a timestamp from cloud firestore and store it in a date variable but I am getting this exception :

java.util.HashMap cannot be cast to java.util.Date

this is the code for getting the value from the cloud firestore :

Date timeStamp = (Date) dataSnapshot.get("date");

and this is the code for storing the date in the database :

map.put("date", ServerValue.TIMESTAMP);

I can't figure out how to solve this error

Mostafa
  • 71
  • 1
  • 9
  • 1
    What are you trying to do with this statement? `map.put("date", ServerValue.TIMESTAMP);` – Andreas Dec 03 '18 at 22:13
  • If you want to insert current time, to be retrieved back as a `java.util.Date` object, why not `map.put("date", new Date());`? – Andreas Dec 03 '18 at 22:16

1 Answers1

1

ServerValue.TIMESTAMP is of type HashMap<String,String>. That's what you are saving here: map.put("date", ServerValue.TIMESTAMP);. When you do dataSnapshot.get("date");, that's what will be returned. If you stored date as a string, you'll have to manually get it from the HashMap.

Ramanlfc
  • 8,283
  • 1
  • 18
  • 24