0

I am saving current date in timestamp value in my firebase with below code

 userValues.put("p_date", ServerValue.TIMESTAMP);

what I need to do is show a time difference between above saved value and current timestamp value, I have tried below code

//time ago
        String starttime = book.getP_date().toString(); //getting saved timestamp from firebase

        //current time
        Long tsLong = System.currentTimeMillis()/1000;
        String endtime = tsLong.toString();

        long diffTime = Long.parseLong(endtime) - Long.parseLong(starttime);
        String elapsedtime = new SimpleDateFormat("hh:mm:ss").format(Long.parseLong(String.valueOf(diffTime)));

        tvTimeAgo.setText(elapsedtime+"ago");

it is giving a result like 07:59:56 ago shown time difference is returning wrong result also what I need to have is 0h 5min 3s ago how can I achieve this?

Mithu
  • 665
  • 1
  • 8
  • 38

1 Answers1

0

You can use TimeUnit.MILLISECONDS for formate TimeStamp as below

String timeHH_MM_SS = String.format("%02d:%02d:%02d", 
    TimeUnit.MILLISECONDS.toHours(diffTime),
    TimeUnit.MILLISECONDS.toMinutes(diffTime) % TimeUnit.HOURS.toMinutes(1),
    TimeUnit.MILLISECONDS.toSeconds(diffTime) % TimeUnit.MINUTES.toSeconds(1));

    tvTimeAgo.setText("Time in hh:mm:ss is: "+timeHH_MM_SS);
Sanjay Bhalani
  • 2,424
  • 18
  • 44