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

Course Queries Syllabus Queries 3 years ago

6.52K 2 0 0 0

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 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());
        }
0 views
0 shares

profilepic.png
manpreet 3 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.

Similar Forum