从Firebase中将日期和时间填入文本视图 [英] Populate Date and Time into Textview from Firebase
问题描述
我创建了一个包含两个文本视图的列表视图,我希望将添加到数据库的任何数据的特定日期和时间从firebase中获取到文本视图中。
任何方法来实现上述?
谢谢。
您的数据结构应该如下所示:
data:{
id1: {
text:some text,
timestamp:1472861629000
},
id2:{
text:some text ,
timestamp:1472861629000
},
...
}
要保存时间戳,可以传递一个firebase常量 ServerValue.TIMESTAMP
,它将根据firebase服务器时间转换为epoch时间
映射< String,Object> values = new HashMap<>();
values.put(text,some text);
values.put(timestamp,ServerValue.TIMESTAMP);
Database.ref()。child(data)。push()。setValue(values);
要将纪元时间转换为人类可读的日期,请使用此方法
public static String epochToFormatted(Long epoch){
SimpleDateFormat sdf = new SimpleDateFormat(dd MM yyyy);
return sdf.format(new Date(epoch));
}
I created a listview with two textviews, i want to get the date and time specific to any data added to the database into the textview from firebase.
Is there any method to achieve the aforementioned?
Thanks.
You need to add another child to store the timestamp when saving the data.
Your data structure should look something like this
"data": {
"id1": {
"text": "some text",
"timestamp": 1472861629000
},
"id2": {
"text": "some text",
"timestamp": 1472861629000
},
"..."
}
To save the timestamp, you can pass a firebase constant ServerValue.TIMESTAMP
that will be converted to an epoch time based on firebase server time
Map<String, Object> values = new HashMap<>();
values.put("text", "some text");
values.put("timestamp", ServerValue.TIMESTAMP);
Database.ref().child("data").push().setValue(values);
To convert the epoch time to human readable date, use this method
public static String epochToFormatted(Long epoch) {
SimpleDateFormat sdf = new SimpleDateFormat("dd MM yyyy");
return sdf.format(new Date(epoch));
}
这篇关于从Firebase中将日期和时间填入文本视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!