如何使Cloud Firestore可读的时间戳记?

时间:2019-12-04 15:35:32

标签: android google-cloud-firestore

因此,以下是我的Cloud Firestore的数据库。该DatePosted显示2019年12月2日UTC +8晚上8.19。根据我使用的代码运行我的应用程序,它显示了一个无法读取的数字。我如何从Cloud Firebase检索数据是否正确?如果不正确,如何获取该时间戳并使之可读?

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;
    }
}

1 个答案:

答案 0 :(得分:1)

查询带有时间戳字段的文档时,将返回一个Timestamp对象。如果您希望使用Date对象,则可以使用其toDate方法。

您将需要编写一些代码来格式化此格式以便显示。对于移动和Web应用程序来说,这是非常常见的任务。