【问题标题】:Handling time in FirebaseFirebase 中的处理时间
【发布时间】:2017-02-02 12:14:54
【问题描述】:

如何在 Firebase 中处理时间?

【问题讨论】:

  • 听起来你正在寻找这个:stackoverflow.com/questions/36658833/… 或这个:stackoverflow.com/questions/33096128/… 或这个:stackoverflow.com/questions/37864974/…(我的搜索找到这些:stackoverflow.com/…
  • @FrankvanPuffelen 感谢您的回复!我试过代码from your answer。不幸的是,这会从数据库中删除所有消息,并且我多次收到时间戳。你能发布一个完整的答案吗?我想在我的问题中描述的消息上添加时间戳。从我之前的 Firebase 问题中可以看出,我已经为这个问题苦苦挣扎了 20 多天。太复杂了! :)
  • @FrankvanPuffelen 感谢您的宝贵时间。我很感激。
  • @FrankvanPuffelen 我已经编辑了我的问题并添加了对 FriendlyMessage.java 的修改 - 我应该对 MainActivity.java 进行哪些更改以实现我想要的?
  • @FrankvanPuffelen 因为以下给我一个错误:FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString(), mUsername, mPhotoUrl, ServerValue.TIMESTAMP);

标签: android firebase firebase-realtime-database timestamp


【解决方案1】:

在模型类FriendlyMessage中添加另一个字段名为:

String timeStamp;
public FriendlyMessage(String text, String name, String photoUrl, String timeStamp) {
    this.text = text;
    this.name = name;
    this.photoUrl = photoUrl;
    this.timeStamp = timeStamp;
}

并且在调用构造函数时以您喜欢的格式传递时间戳:

mSendButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString(), mUsername,
                mPhotoUrl, /*time stamp in the format you like*/ timeStamp);
        mFirebaseDatabaseReference.child(MESSAGES_CHILD).push().setValue(friendlyMessage);
        mMessageEditText.setText("");
        mFirebaseAnalytics.logEvent(MESSAGE_SENT_EVENT, null);
    }
});

【讨论】:

  • 我已经这样做了,您认为它更复杂 - 请随时查看整个项目 here 并测试您的建议。你会看到你会得到各种我不能在这里发布的错误,因为它们太多太长了。不过感谢您的回答。
  • 真的吗?然后请发布您的完整工作代码。
  • 您需要从 firebase 中删除旧数据库,然后再次运行您的代码。即使我给你我的代码,我也在做我上面写的一模一样的事情。
猜你喜欢
  • 2021-01-17
  • 2020-05-11
  • 2016-10-04
  • 1970-01-01
  • 2017-05-02
  • 1970-01-01
  • 1970-01-01
  • 2014-02-18
  • 1970-01-01
相关资源
最近更新 更多