简体   繁体   中英

How to retrieve date and time in a RecyclerView from FirebaseFirestore if i have stored time as ServerValue.TIMESTAMP in the database

I am making an app in which user can post information and while posting I am saving the time of the post by using ServerValue.TIMESTAMP in the Firestore database.

The problem is that the time is getting saved in the database as map something like this

time:
   .sv : "timestamp"

Here is a picture to my database at Firestore 在最右边的列中,您可以看到我的时间如何保存在数据库中

Now I want to extract human readable date and time to display to the users in a RecyclerView .

This is my model class through which i want to get the time but haven't included any data member yet because i don't know how to get it done.

public class PostDisplayModel extends TransferId{

public String teacherName;
public String topic;

PostDisplayModel(){}

public PostDisplayModel(String teacherName, String topic) {
    this.teacherName = teacherName;
    this.topic = topic;
}

public String getTeacherName() {
    return teacherName;
}

public void setTeacherName(String teacherName) {
    this.teacherName = teacherName;
}

public String getTopic() {
    return topic;
}

public void setTopic(String topic) {
    this.topic = topic;
}
}

Below is my Adapter Class

public class PostDisplayAdapter extends RecyclerView.Adapter<PostDisplayAdapter.ViewHolder> {

Context context;
List<PostDisplayModel> postsList;

PostDisplayAdapter(Context context,List<PostDisplayModel> postsList){
    this.postsList = postsList;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_display_blueprint,parent,false);
    return  new ViewHolder(view);
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {

    holder.headline.setText(postsList.get(position).getTopic());
    holder.uploaderName.setText(postsList.get(position).getTeacherName());

    final String id = postsList.get(position).id;
    holder.mView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(context,PostDisplayActivity.class);
            intent.putExtra("id",id);
            context.startActivity(intent);

        }
    });
}

@Override
public int getItemCount() {
    return postsList.size();
}

class ViewHolder extends RecyclerView.ViewHolder{

    View mView;
    public TextView uploaderName, headline;
    public TextView time;         //this is the textview in which i want the time to get displayed

    public ViewHolder(View itemView) {
        super(itemView);
        mView = itemView;

        uploaderName = mView.findViewById(R.id.uploader_name);
        headline = mView.findViewById(R.id.headline);
        time = mView.findViewById(R.id.time);     //getting id of the time textview

    }
}
}

I want a correct format of that timestamp like yyyy-MM-dd hh:mm:ss in textview, I am getting every other data correctly from the recycler view.

Any sort of suggestion/help will be appreciated.

Thanks in advance.

This is happening, because you are setting the timestamp as ServerValue.TIMESTAMP . This is the right way when you are using a Firebase Realtime database. In case of Cloud Firestore, you need to set it as a FieldValue.serverTimestamp() .

This is the correct way in which you can set the TIMESTAMP in a Cloud Firestore database using a model class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM