user7593511 user7593511 - 1 year ago 81
Android Question

How to get Time at which data is posted in firebase

I am making an App which contains RecyclerView with CardView.
When the data is posted in Firebase, it should show the time in RecyclerView.
For e.g. Posted 4 hrs ago, posted few mins ago , etc...

How to achieve it?

Answer Source

It is very possible. It would have been best if you provided a code snippet for edit suggestions. Use mine in this case. I will make a post with a title and time.

In your adapter where you firebase post is processed, add the time like this

public class post {

private String Title;
private long Time;

public post(String title){
    Title = title;
    Time = new Date().getTime();
}

public post(){}

public String getTitle() {return Title;}
public void setTitle(String title) {this.Title = title;}
public long getTime() {return Time;}
public void setTime(long time) {this.Time = time;}

}

now when you make your post, you will have values title and time in milliseconds and you will not have to call time. It is added automatically.

FirebaseDatabase.getInstance()
                .getReference(staticPath)
                .push()
                .setValue(new post(Title));

when showing your data on recyclerview, you will get the data normally

viewHolder.showPosts(model.getTitle(), model.getTime());

and set time to in the example you showed like

timeText.setText(DateUtils.getRelativeTimeSpanString(time,
                    System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download