Been working on this timestamp for ages and using numerous posts from StackOverflow to help me through it. Finally got timestamp saving in Firebase along with all the other children for the pictures, but I can't get it to come upon the app in the TextView.
So in my PostActivity.java class I have used Map to save the timestamp to Firebase, and then in my PostAdapter.java class I have the ViewHolders so that it appears in the RecyclerView in my HomeFragment, but it's coming out as a Long value 41526276373and not in SimpleDateFormat as it should.
Can someone help me?
Below you have my PostActivity.java class and PostAdapter.java class.
PostActivity.java
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts");
String postid = reference.push().getKey();
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("postid", postid);
hashMap.put("postimage", myUrl);
hashMap.put("description", txt_description.getText().toString());
hashMap.put("text_event", txt_event.getText().toString());
hashMap.put("text_location", txt_location.getText().toString());
hashMap.put("text_date_time", txt_date_time.getText().toString());
hashMap.put("publisher", FirebaseAuth.getInstance().getCurrentUser().getUid());
hashMap.put("timestamp", ServerValue.TIMESTAMP);
reference.child(postid).setValue(hashMap);
//Do I have to put here reference.updateChildren(hashmap)?
progressDialog.dismiss();
startActivity(new Intent(PostActivity.this, MainActivity.class));
finish();
} else {
Toast.makeText(PostActivity.this, "Unsuccessful. Try again", Toast.LENGTH_SHORT).show();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(PostActivity.this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
} else {
Toast.makeText(this, "No Image Selected", Toast.LENGTH_SHORT).show();
}
}
public static String getTimeDate(long timestamp) {
try {
DateFormat dateFormat = DateFormat.getDateTimeInstance();
Date netDate = (new Date(timestamp));
return dateFormat.format(netDate);
} catch (Exception e){
return "timestamp";
}
}
PostAdapter.java
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.post_item, parent, false);
return new PostAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
final Post post = mPost.get(position);
Glide.with(mContext).load(post.getPostimage())
.apply(new RequestOptions().placeholder(R.drawable.placeholderimg))
.into(holder.post_image);
if ("".equals(post.getTimestamp())) {
holder.timestamp.setVisibility(View.GONE);
} else {
if (post.getPostid() != null) {
holder.timestamp.setVisibility(View.VISIBLE);
holder.timestamp.setText(post.getTimestamp().toString());
}
}
PostAdapter.java Updated
if ("".equals(post.getTimestamp())) {
holder.timestamp.setVisibility(View.GONE);
} else {
if (post.getPostid() != null) {
holder.timestamp.setVisibility(View.VISIBLE);
***holder.timestamp.setText(post.getTimeDate());***
}
}
***getTimeDate(post.getPostid(), holder.timestamp);***
public String getTimeDate(long timestamp) {
try {
DateFormat dateFormat = DateFormat.getDateTimeInstance();
Date netDate = (new Date(this.timestamp));
return dateFormat.format(netDate);
} catch (Exception e) {
throw e;
}
}