如何以毫秒为单位获取Firebase服务器的当前时间戳? [英] How to get current timestamp of firebase server in milliseconds?

查看:166
本文介绍了如何以毫秒为单位获取Firebase服务器的当前时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得firebase当前的时间戳,不能找到一个方法来获得。我想要保存图像中显示的孩子。为此,我必须得到时间戳并相应地得到日期。请帮助...



ServerValue.TIMESTAMP 来设置服务器的时间。 $ c>它是一个 Map<字符串,字符串> 类型和 {。sv:timestamp} 对。当它被发送到firebase数据库时,它将被转换为一个长的 Unix纪元时间像这样 1469554720



所以问题是,你不能直接设置这个键。最好的办法是把时间戳记放入你的对象中,并使用 DatabaseReference.push()来获得有保证的唯一键。



例如

  DatabaseReference ref = FirebaseDatabase.getInstance()。getReference(); 
String key = ref.push()。getKey(); //这将创建一个新的唯一键
Map< String,Object> value = new HashMap<>();
value.put(name,shesh);
value.put(address,lucknow);
value.put(timestamp,ServerValue.TIMESTAMP);
ref.child(key).setValue(value);

如果您想以该格式(dd-mm-yyyy)保存,则会出现黑客攻击这不被推荐。您需要先将它(ServerValue.TIMESTAMP)保存到另一个临时节点,然后使用 Date class将其转换为该格式,然后检索时间戳。


I wanted to get firebase current timestamp, not able to find a way to get that. I want to save child as shown in the image. And for that I've to get timestamp and get the date accordingly. Please help...

解决方案

You can set the server time by using ServerValue.TIMESTAMP which is a Map<String, String> type with {".sv" : "timestamp"} pair. When it's sent to the firebase database, it will be converted to a Long Unix epoch time like this 1469554720.

So the problem is, you can't set this as a key directly. The best approach is to put the timestamp inside your object and use DatabaseReference.push() to get the guaranteed unique key.

For example

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
String key = ref.push().getKey(); // this will create a new unique key
Map<String, Object> value = new HashMap<>();
value.put("name", "shesh");
value.put("address", "lucknow");
value.put("timestamp", ServerValue.TIMESTAMP);
ref.child(key).setValue(value);

If you want to save it with that format (dd-mm-yyyy), there's a hack but this is not recommended. You need to save it first (ServerValue.TIMESTAMP) to another temporary node, and then retrieve the timestamp before convert it into that format using Date class.

这篇关于如何以毫秒为单位获取Firebase服务器的当前时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆