且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法将java.lang.Long类型的对象转换为java.util.Date类型

更新时间:2023-01-17 22:07:29

Firebase不支持 Date 类对象,因此您需要将它们存储为您已经完成的long / timestamp。



1463845578489 是一个长整型,它需要存储在一个long变量中,而不是一个Date



  private long publishedDate; 

然后,将long转换为有效的Date对象,您可以使用这个

  Date d = new Date(publishedDate); 


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

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.

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);