0

I have developed an application where the user receives the message from other application user. I want to just show the time like Facebook, eg. 1sec ago or 3Hrs ago. Something in this fashion. I tried a code from one of our Fellow S.O expert but that code seems to misbehave. Here's the code which i used in my app.

static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    static int[] steps = { 1, 60, 3600, 3600 * 24 };
    static String[] names = { "sec", "mins", "hrs", "days" };
public static String formatDelay(String date) {
        try {
            Date d = sdf.parse(date);
            Long stamp = d.getTime();
            Calendar c = Calendar.getInstance();

             Long now = System.currentTimeMillis() / 1000;

            Format format = new SimpleDateFormat("yyyy MM dd HH:mm:ss");
            String time = format.format(now);
            Long dif = now - stamp;
            dif = dif / 1000;
            if (stamp > now)
                return "";

            for (int i = 0; i < steps.length; i++) {
                if (dif < steps[i]) {
                    String output = Long.toString(dif / steps[i - 1]) + " "
                            + names[i - 1];
                    return output;
                }
            }

            return Long.toString(dif / steps[3]) + " " + names[3];

        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

When I used this code and sent a message from my application , it should show me sent 1sec ago, but in my case it shows me wrong time delay. For eg. I sent the message at 6pm then when I check my application sent item at 6:15pm its should show me 15 mins ago. But it shows me 12 hrs. When i debugged code, got to know that now time show date as 1970 00:00:00, this is because Long now = System.currentTimeMillis() / 1000; when i remove that /1000 it shows me correct date and time. I am clue less why this is happening please help.

Mayank
  • 165
  • 1
  • 5
  • 20
  • System::currentTimeMillis returns (who would guess?) time in milliseconds, so dividing by 1000 returns you value in seconds. It's obviously odd, because format.format() is parametrized with milliseconds, not seconds. So removing /1000 was the right step – Semyon Danilov Aug 01 '14 at 12:15

3 Answers3

1

Use

android.text.format.DateUtils.getRelativeTimeSpanString (long time, long now, long minResolution, int flags) 

this will return time span in String format

For eg. if you pass a long value corresponding to 42 minutes ago in time and flags as DateUtils.FORMAT_ABBREV_RELATIVE the method will return 42 minutes ago

Official documentation DateUtils.getRelativeTimeSpanString

Apoorv
  • 13,470
  • 4
  • 27
  • 33
0

I would recommend using Joda-Time API. See this answer for reference.

Community
  • 1
  • 1
EyesClear
  • 28,077
  • 7
  • 32
  • 43
0

When you divide System.currentTimeMillis() by 1000 you are converting its value to seconds.

You're then using that value to create a date which is interpreting the seconds value as the milliseconds since Thu Jan 01 1970, hence the date difference.

Daniel
  • 777
  • 9
  • 21
  • Can you tell me what can be the change that has to be done in the function of the question i have asked – Mayank Aug 01 '14 at 13:41