tomss
tomss

Reputation: 371

X minutes and Y seconds ago

I have made this line of code:

 (DateUtils.getRelativeDateTimeString(getActivity().getApplicationContext(), seconds, DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, DateUtils.FORMAT_ABBREV_TIME)).toString();

When 3 seconds have passed it returns:

3 seconds ago

When 125 seconds have passed it returns:

2 minutes ago

Is possible to change this code to return:

2 minutes and 5 seconds ago 

Instead of 2 minutes ago? How?

Thanks.

Upvotes: 2

Views: 346

Answers (1)

Sanoop Surendran
Sanoop Surendran

Reputation: 3486

  String.format(Locale.getDefault(), " %d min: %d sec ago",
                                TimeUnit.MILLISECONDS.toMinutes(millis),
                                TimeUnit.MILLISECONDS.toSeconds(millis) - 
                                          TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)))

which retruns the time in format like for eg 2 min : 15 sec ago where millis is the time in millisecond

EDIT: for days and hours

  int day = (int) TimeUnit.SECONDS.toDays(millis);
  long hours = TimeUnit.SECONDS.toHours(millis) - (day * 24);
  long minutes = TimeUnit.SECONDS.toMinutes(millis) - (TimeUnit.SECONDS.toHours(millis) * 60);
  long seconds = TimeUnit.SECONDS.toSeconds(millis) - (TimeUnit.SECONDS.toMinutes(millis) * 60);

  String time = String.valueOf(day) + " Days " + String.valueOf(hours) + " Hours " +
          String.valueOf(minutes) + " Minutes " + String.valueOf(seconds) + " Seconds.";

This way you can get days and even years..

Upvotes: 2

Related Questions