栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java当我向Firebase Realtime数据库添加新值时如何保存当前日期/时间

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java当我向Firebase Realtime数据库添加新值时如何保存当前日期/时间

最佳做法是TIMESTAMP像这样保存数据

ServerValue.TIMESTAMP

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();Map map = new HashMap();map.put("timestamp", ServerValue.TIMESTAMP);ref.child("yourNode").updateChildren(map);

还要记住,将设置为时,将其

TIMESTAMP
设置为
Map
,但是将其获取时,则将其设置为Long。为了获取数据,我建议您使用以下方法:

public static String getTimeDate(long timestamp){    try{        DateFormat dateFormat = getDateTimeInstance();        Date netDate = (new Date(timestamp));        return dateFormat.format(netDate);    } catch(Exception e) {        return "date";    }}

编辑:模型类应如下所示:

public class YourModelClass {    //private fields    private Map<String, String> timestamp;    public YourModelClass() {}    //public setters and getters for the fields    public void setTimestamp(Map<String, String> timeStamp) {this.timestamp= timestamp;}    public Map<String, String> getTimestamp() {return timestamp;}}

请记住,

ServerValue.TIMESTAMPFirebase Realtime Database
只是一个令牌,当它在写入操作期间用作子值时,它将转换为服务器端的数字。该日期仅在写入操作完成后才出现在数据库中。

要获取

timestamp
,还有另一种方法,那就是在
Cloud Functions for Firebase
中编写一个
fronction
,它很简单:

exports.currentTime = functions.https.onRequest((req, res) => {    res.send({"timestamp":new Date().getTime()})})

您可以将其托管在Cloud Function中,无需用户干预即可获取服务器时间戳。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/517634.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号