How to convert date and time string to time ago type?

Course Queries Syllabus Queries 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I am new to in firebase and android also. In my app students can share his question and answer about syllabus. For this i set the time time and date string in firebase database and by the POJO class i retrieve the date and time in String format like Date: 06-April-2019 and time:01:38:24. Till now i used setText in TextView by model.getTime and model.getDate. So this like be a update(TextView) 06-April-2019 01:38:24 now i want to chnage it by Timeago like minute ago, hour ago, day ago, month ago, year ago

Sorry for bad english. and thanx in advance

protected void onBindViewHolder(@NonNull  UserViewHolder holder, int position, @NonNull final question model)
        {

final String PostKey=getRef(position).getKey();//get key by this line

        holder.userfullname.setText(model.getFirstname()+" "+model.getLastname());
        holder.time.setText(" "+model.getTime());//***TextView 06-April-2019*** 
        holder.date.setText(" "+model.getDate());//*TextView 01:38:24*
        holder.description.setText(model.getDescription());
        }
profilepic.png
manpreet 2 years ago

 

With System.currentTimeMillis(); you get the Milliseconds they have passed since 1/1/1970 as long.

So what u wand we can achieve with 2 more custom functions in your adapter.

The first one take ur date and time and convert it to miliseconds.

 

private long millisFromDateAndTime(String date, String time){
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMMM-yyyy");
        SimpleDateFormat timeFormat = new SimpleDateFormat("kk:mm:ss");
        try
        {
            Date mDate = dateFormat.parse(date);
            Date mTime = timeFormat.parse(time);
            long dateInMilliseconds = mDate.getTime();
            long timeInMilliseconds = mTime.getTime();
            return timeInMilliseconds + dateInMilliseconds;
        }
        catch (ParseException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return 0;
    }

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.