简体   繁体   English

从Firebase将日期和时间填充到Textview中

[英]Populate Date and Time into Textview from Firebase

I created a listview with two textviews, i want to get the date and time specific to any data added to the database into the textview from firebase. 我创建了一个具有两个textviews的listview,我想要获取特定于特定日期和时间的数据,这些数据是从firebase到textview中添加到数据库的任何数据的。

Is there any method to achieve the aforementioned? 有什么方法可以实现上述目的?

Thanks. 谢谢。

You need to add another child to store the timestamp when saving the data. 保存数据时,您需要添加另一个子项来存储时间戳。

Your data structure should look something like this 您的数据结构应如下所示

"data": {
    "id1": {
        "text": "some text",
        "timestamp": 1472861629000
    },
    "id2": {
        "text": "some text",
        "timestamp": 1472861629000
    },
    "..."
}

To save the timestamp, you can pass a firebase constant ServerValue.TIMESTAMP that will be converted to an epoch time based on firebase server time 要保存时间戳,您可以传递ServerValue.TIMESTAMP常量ServerValue.TIMESTAMP ,该常量将根据ServerValue.TIMESTAMP服务器时间转换为纪元时间

Map<String, Object> values = new HashMap<>();
values.put("text", "some text");
values.put("timestamp", ServerValue.TIMESTAMP);
Database.ref().child("data").push().setValue(values);

To convert the epoch time to human readable date, use this method 要将时代时间转换为人类可读的日期,请使用此方法

public static String epochToFormatted(Long epoch) {
    SimpleDateFormat sdf = new SimpleDateFormat("dd MM yyyy");
    return sdf.format(new Date(epoch));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM