Java 向 Firebase 实时数据库添加新值时如何保存当前日期/时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43584244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save the current date/time when I add new value to Firebase Realtime Database
提问by Leenah
I want to save the current date/time in specific field when I add new value to Firebase Realtime Database via control panel.
当我通过控制面板向 Firebase 实时数据库添加新值时,我想在特定字段中保存当前日期/时间。
How can I achieve that?
我怎样才能做到这一点?
Please help me.
请帮我。
采纳答案by Alex Mamo
The best practice is to save your data as a TIMESTAMP
like this ServerValue.TIMESTAMP
.
最佳做法是将您的数据保存为TIMESTAMP
这样的ServerValue.TIMESTAMP
。
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
Map map = new HashMap();
map.put("timestamp", ServerValue.TIMESTAMP);
ref.child("yourNode").updateChildren(map);
Also remember, when you set the TIMESTAMP
, you set it as a Map
, but when you retrieve it, you retrieve it as a Long
. To get the data back, i suggest you use this method:
还要记住,当您设置 时TIMESTAMP
,您将其设置为 a 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";
}
}
Edit:The model class should look like this:
编辑:模型类应如下所示:
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;}
}
Remember, ServerValue.TIMESTAMP
is just a token that Firebase Realtime Database converts to a number on server side when it's used as a child value during write operation. The date only appears in the database after the write operation completes.
请记住,ServerValue.TIMESTAMP
它只是 Firebase 实时数据库在写入操作期间用作子值时在服务器端转换为数字的令牌。日期仅在写入操作完成后出现在数据库中。
To get the timestamp
, there is also another approach, which would be to write a frunction in Cloud Functions for Firebaseand it will be as easy as:
要获得timestamp
,还有另一种方法,那就是在Cloud Functions 中为 Firebase编写一个函数,它会像以下一样简单:
exports.currentTime = functions.https.onRequest((req, res) => {
res.send({"timestamp":new Date().getTime()})
})
You can host this in Cloud Function and get the server timestamp without user interaction.
您可以在 Cloud Function 中托管它并在没有用户交互的情况下获取服务器时间戳。
回答by Mitul Gedeeya
Use data type Number into Firebase.
在 Firebase 中使用数据类型 Number。
- Store current millis while storing data into Fireabase.
- While retrieving data you will get millis which you were stored previously. Parse in into Java Date Instance and work further.
- 在将数据存储到 Fireabase 的同时存储当前毫秒。
- 在检索数据时,您将获得之前存储的毫秒数。解析为 Java 日期实例并进一步工作。
回答by Goodlife
Alex Mamo is right but then this is how you should pass incase you call it from a model class
Alex Mamo 是对的,但是如果您从模型类中调用它,您应该如何传递它
public class FirebMessage {
public String message;
public String senderPhoneNumber;
public String receiverPhoneNumber;
public Map time;
public FirebMessage() {
}
public FirebMessage(String message, String senderPhoneNumber, String receiverPhoneNumber, Map time) {
this.message = message;
this.senderPhoneNumber = senderPhoneNumber;
this.receiverPhoneNumber = receiverPhoneNumber;
this.time = time;
}
Then consume as follows
然后消费如下
private void writeNewMessage(String message, String receiver,String sender) {
FirebMessage firebMessage = new FirebMessage(message, receiver,sender,ServerValue.TIMESTAMP);
mDatabase.child("messages").push().setValue(firebMessage, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if (databaseError != null) {
Log.e(TAG,"Data Not saved");
} else {
Log.e(TAG,"Data saved successfully");
}
}
});
}