millinet millinet - 5 months ago 51
Android Question

Android - Firebase - Can't convert object of type java.lang.Long to type java.util.Date

I'm migrating to Firebase new version and I get the following error when trying to getvalue from the snapshot

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Long to type java.util.Date


This happens when trying to getvalue from the snapshot in the following method

public void getUpdates(DataSnapshot dataSnapshot){
Item item = dataSnapshot.getValue(Item.class);
itemArrayList.add(item);
itemAdapter.refreshItem(itemArrayList);
}


I guess it has something to do with the Item object, but it worked before, so I can't figure out what's wrong. I'm using dates indeed.

Firebase item structure

Firebase item structure

Item object

private String title;
private String description;
private HashMap<String, ItemPicture> picturesHashMap;
private Date publishedDate;
private Date deletionDate;
private String condition;
private String delivery;
private String uid;
private int reported;
private boolean given;
private Location location;
private String frontImage;
private String uniqueID;


Any help would be highly appreciated.

Answer

Firebase doesn't support Date class objects so you need to store them as long/timestamp which you have already done.

1463845578489 is a long which needs to be stored in a long variable and not a Date

change your variable declaration to

private long publishedDate;

Then to convert long to a valid Date object you can use this

Date d = new Date(publishedDate);