How do I make readable TimeStamp from Cloud Firestore?

So below are the database of my cloud firestore. That DatePosted shows 2nd December, 2019 at 8.19 pm UTC +8. According to the code I used and I run my app, it shows an unreadable number. Is it correct on how I retrieve the data from cloud firebase? If its wrong, how do I retrieve that timestamp and make it readable?

Cloud Firestore Database Screenshot of my app

ForumAdapter.java

public class ForumAdapter extends FirestoreRecyclerAdapter<Forum,ForumAdapter.ForumHolder> {
    private OnItemClickListener listener;

    public ForumAdapter(FirestoreRecyclerOptions<Forum> options) {
        super(options);
    }

    @Override
    public void onBindViewHolder(ForumHolder forumHolder, int i, Forum forum) {
        forumHolder.textViewTitle.setText(forum.getTitle());
        forumHolder.textViewDescription.setText(forum.getDescription());
        forumHolder.timeStamp.setText(forum.getDatePosted().toString());
    }

    @NonNull
    @Override
    public ForumHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        android.view.View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardviewforumtitle,parent,false);
        return new ForumHolder(v);
    }
    class ForumHolder extends RecyclerView.ViewHolder{
        TextView textViewTitle;
        TextView textViewDescription;
        TextView timeStamp;
        public ForumHolder(View itemView) {
            super(itemView);
            textViewTitle = itemView.findViewById(R.id.tvTitle);
            textViewDescription = itemView.findViewById(R.id.tvDescription);
            timeStamp = itemView.findViewById(R.id.tvTimestamp);

            textViewTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    // NO_POSITION to prevent app crash when click -1 index
                    if(position != RecyclerView.NO_POSITION && listener !=null ){
                        listener.onItemClick(getSnapshots().getSnapshot(position),position);
                    }
                }
            });
        }
    }
    public interface OnItemClickListener{
        void onItemClick(DocumentSnapshot documentSnapshot, int position);
    }
    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener = listener;
    }
    @Override
    public int getItemCount() {
        return super.getItemCount();
    }
}

Forum.java

public class Forum {
    private String Title;
    private String Description;
    private String User;
    private com.google.firebase.Timestamp DatePosted;

    public Forum() {
    }

    public Forum(String title, String description, Timestamp datePosted,String user) {
        Title = title;
        Description = description;
        DatePosted = datePosted;
        User = user;
    }

    public String getUser() {
        return User;
    }

    public void setUser(String user) {
        User = user;
    }

    public String getTitle() {
        return Title;
    }

    public void setTitle(String title) {
        Title = title;
    }

    public String getDescription() {
        return Description;
    }

    public void setDescription(String description) {
        Description = description;
    }

    public Timestamp getDatePosted() {
        return DatePosted;
    }

    public void setDatePosted(Timestamp datePosted) {
        DatePosted = datePosted;
    }
}

Upvotes: 1

Views: 161

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317342

When you query a document with a timestamp field, you will get a Timestamp object back. If you'd rather have a Date object, you can use its toDate method.

You will need to write some code to format this for display. This is a very common task for mobile and web apps.

Upvotes: 2

Related Questions