【问题标题】:Populate Date and Time into Textview from Firebase将日期和时间从 Firebase 填充到 Textview
【发布时间】:2017-01-11 00:39:43
【问题描述】:

我创建了一个包含两个文本视图的列表视图,我想将添加到数据库的任何数据的特定日期和时间从 firebase 获取到文本视图中。

有什么方法可以实现上述吗?

谢谢。

【问题讨论】:

    标签: android firebase timestamp firebase-realtime-database


    【解决方案1】:

    保存数据时需要再添加一个child来存储时间戳。

    你的数据结构应该是这样的

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

    要保存时间戳,您可以传递一个 firebase 常量 ServerValue.TIMESTAMP,它将根据 firebase 服务器时间转换为纪元时间

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

    要将纪元时间转换为人类可读的日期,请使用此方法

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

    【讨论】:

    • 谢谢@Wilik,让我试一试,我会尽快回复您。
    • 确定@KennyDabiri,如果您有问题请告诉我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-06
    • 2020-11-07
    • 2013-01-16
    相关资源
    最近更新 更多