我已经找到了一些关于从Firebase检索和转换时间戳的示例。只是因为我无法理解转换是如何工作的,它总是给我一个关键和值的输出,而不是正确的日期格式。我只是一个新手程序员,正在构建我的第一个移动应用程序。
这是我的模型类UserPost中的代码。
public class UserPost {
String postEmotion;
String authorName;
String userId;
int postEmotion;
String postDescription;
HashMap<String, Object> dateCreated;
public UserPost() {
}
public UserPost(String authorName, String userId, int postEmotion, String postDescription, HashMap<String,Object> dateCreated) {
this.authorName = authorName;
this.userId = userId;
this.postEmotion = postEmotion;
this.postDescription = postDescription;
this.dateCreated = dateCreated;
HashMap<String, Object> timestampNow = new HashMap<>();
timestampNow.put("dateCreated", ServerValue.TIMESTAMP);
}
public String getAuthorName() { return authorName; }
public String getUserId(){ return userId; }
public int getPostEmotion() {
return postEmotion;
}
public String getPostDescription() {
return postDescription;
}
public HashMap<String, Object> getDateCreated() {
//If there is a dateCreated object already, then return that
if (dateCreated != null) {
return dateCreated;
}
//Otherwise make a new object set to ServerValue.TIMESTAMP
HashMap<String, Object> dateCreatedObj = new HashMap<String, Object>();
dateCreatedObj.put("dateCreated", ServerValue.TIMESTAMP);
//convertTime(getDateCreatedLong());
return dateCreatedObj;
}
@Exclude
public long getDateCreatedLong(){
long timeStampvalue = (long)dateCreated.get("dateCreated");
convertTime(timeStampvalue);
return timeStampvalue;
}
public void convertTime(Long unixtime) {
Date dateObject = new Date(unixtime);
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MM-yy hh:mm:ss");
dateFormatter.format(dateObject);
}
@Exclude
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put("userId", userId);
result.put("postEmotion", postEmotion);
result.put("authorName",authorName);
result.put("postDescription", postDescription);
result.put("dateCreated", dateCreated);
return result;
}
}
NewsFeed.class我从Firebase检索数据。
mDatabase.child("post").addChildEventListener(new ChildEventListener() {
public static final String TAG = "MARK's error";
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot != null && dataSnapshot.getValue() != null) {
try{
UserPost userPost = dataSnapshot.getValue(UserPost.class);
String author = userPost.getAuthorName();
String uid = userPost.getUserId();
int emo = userPost.getPostEmotion();
String post = userPost.getPostDescription();
HashMap<String,Object> date = userPost.getDateCreated();
data.add(new UserPost(author,uid,emo,post,date));
} catch (Exception ex) {
Log.e(TAG, ex.getMessage());
}
}
adapter.notifyDataSetChanged(); //to load the data if there's a value initially on my Firebase DB
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
保存用户帖子。
String key = mDatabase.child("post").push().getKey();
UserPost userPost = new UserPost(author,uid,setemoticon,postData,mDate);
Map<String, Object> postValues = userPost.toMap();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/post/" + key,postValues);
childUpdates.put("/user-post/" + uid + "/" + key, postValues);
mDatabase.updateChildren(childUpdates);
Toast.makeText(getActivity(),"PIN POSTED!", Toast.LENGTH_SHORT).show();