java long转object,无法将java.lang.Long类型的对象转换为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

0eLNN.png

Item object

private String title;

private String description;

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值