Android Firebase数据库的公共时间
我正在为android创建一个聊天应用程序。为此,我正在使用Firebase实时数据库。数据库的“聊天”分支是这样的: 对于使用用户唯一ID(如“513”、“675”等)生成的聊天室,存在唯一ID。在这些聊天室中,存在同样具有唯一ID的消息对象,并在其中存储消息发送日期、发送者姓名和消息文本的信息。消息对象的构造函数如下:Android Firebase数据库的公共时间,android,firebase,time,firebase-realtime-database,Android,Firebase,Time,Firebase Realtime Database,我正在为android创建一个聊天应用程序。为此,我正在使用Firebase实时数据库。数据库的“聊天”分支是这样的: 对于使用用户唯一ID(如“513”、“675”等)生成的聊天室,存在唯一ID。在这些聊天室中,存在同样具有唯一ID的消息对象,并在其中存储消息发送日期、发送者姓名和消息文本的信息。消息对象的构造函数如下: public Message(String text,String senderUID, Long date){ this.text = text;
public Message(String text,String senderUID, Long date){
this.text = text;
this.senderUID = senderUID;
this.date = date;
}
这就是我为每条消息生成时间并将它们发送到firebase数据库的方式
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calendar = Calendar.getInstance();
String second,hour,minute;
String time;
if(calendar.get(Calendar.SECOND)<10){
second = "0"+calendar.get(Calendar.SECOND);
}
else
{
second = ""+calendar.get(Calendar.SECOND);
}
if(calendar.get(Calendar.MINUTE)<10){
minute = "0"+calendar.get(Calendar.MINUTE);
}
else
{
minute = ""+calendar.get(Calendar.MINUTE);
}
if(calendar.get(Calendar.HOUR)<10){
hour = "0"+calendar.get(Calendar.HOUR);
}
else
{
hour = ""+calendar.get(Calendar.HOUR);
}
time = date + hour + minute + second;
Log.d("time",time);
Message message = new Message(messageEditText.getText().toString(), user.getDisplayName(), Long.valueOf(time));
chatRoomDatabaseRef.child(chatID).child(user.getUid() + generateRandomNumber()).setValue(message);
messageEditText.setText("");
}
});
sendButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
calendar=calendar.getInstance();
字符串秒,小时,分钟;
串时间;
if(calendar.get(calendar.SECOND)用作时间
Message message = new Message(messageEditText.getText().toString(), user.getDisplayName(), ServerValue.TIMESTAMP);
为了找回它
private String getDate(long time) {
Calendar cal = Calendar.getInstance(Locale.ENGLISH);
cal.setTimeInMillis(time);
String date = DateFormat.format("dd-MM-yyyy", cal).toString();
return date;
}
用作时间
Message message = new Message(messageEditText.getText().toString(), user.getDisplayName(), ServerValue.TIMESTAMP);
为了找回它
private String getDate(long time) {
Calendar cal = Calendar.getInstance(Locale.ENGLISH);
cal.setTimeInMillis(time);
String date = DateFormat.format("dd-MM-yyyy", cal).toString();
return date;
}
试试这个
firebase
.database()
.ref("/.info/serverTimeOffset")
.on("value", function(offset) {
let offsetVal = offset.val() || 0;
let serverTime = Date.now() + offsetVal;
console.log("serverTime", serverTime);
});
试试这个
firebase
.database()
.ref("/.info/serverTimeOffset")
.on("value", function(offset) {
let offsetVal = offset.val() || 0;
let serverTime = Date.now() + offsetVal;
console.log("serverTime", serverTime);
});
请您再解释一下好吗?在哪里使用ServerValue.TIMESTAMP,以及应该将哪个变量传递给getDate函数?您应该传递从消息中获得的ServerValue.TIMESTAMP,以显示消息类的构造函数包含的日期尽可能长,因此它不接受映射类型变量。我尝试更改类型of date变量在构造函数中,但不起作用。请您再解释一下好吗?在哪里使用ServerValue.TIMESTAMP,以及应该将哪个变量传递给getDate函数?您应该传递从消息中获得的ServerValue.TIMESTAMP,以显示消息类的itConstructor包含尽可能长的日期,因此它不会访问pt是一个映射类型变量。我尝试在构造函数中更改日期变量的类型,但没有成功。