0

i want to show time stamp in my chat app and update this if the message is send before 2 minutes to show 2 minutes ago in timestamp i use this method to show timestamp when message is send but i want to update in every second

Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("h:mm a");
timeStamp = simpleDateFormat.format(calendar.getTime());
Burhan Khanzada
  • 945
  • 9
  • 27

3 Answers3

3

You must have used ListView or RecyclerView for that, you just need to use below method in your adapter to set time in your view.

Note: This will give you updated time when this method will be called, if you user keep scrolling up and down you'll get latest updated time and if user not scrolling in that case it'll not give you latest time.

/*
 * Copyright 2012 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

private static final int SECOND_MILLIS = 1000;
private static final int MINUTE_MILLIS = 60 * SECOND_MILLIS;
private static final int HOUR_MILLIS = 60 * MINUTE_MILLIS;
private static final int DAY_MILLIS = 24 * HOUR_MILLIS;


public static String getTimeAgo(long time, Context ctx) {
    if (time < 1000000000000L) {
        // if timestamp given in seconds, convert to millis
        time *= 1000;
    }

    long now = getCurrentTime(ctx);
    if (time > now || time <= 0) {
        return null;
    }

    // TODO: localize
    final long diff = now - time;
    if (diff < MINUTE_MILLIS) {
        return MINUTE_MILLIS/SECOND_MILLIS;
    } else if (diff < 2 * MINUTE_MILLIS) {
        return "a minute ago";
    } else if (diff < 50 * MINUTE_MILLIS) {
        return diff / MINUTE_MILLIS + " minutes ago";
    } else if (diff < 90 * MINUTE_MILLIS) {
        return "an hour ago";
    } else if (diff < 24 * HOUR_MILLIS) {
        return diff / HOUR_MILLIS + " hours ago";
    } else if (diff < 48 * HOUR_MILLIS) {
        return "yesterday";
    } else {
        return diff / DAY_MILLIS + " days ago";
    }
}

OR
Use one of DateUtils's getRelative* functions as suggested by Sangharsh.

Rikin Prajapati
  • 1,913
  • 2
  • 16
  • 23
  • thanks i have an other rquestion i you want to help me plz – Burhan Khanzada Feb 06 '17 at 13:15
  • 1
    i have made just simple timestamp for my app that is stored as string in databsae but i dont have right method bcz i i change device time its als shoes database timestamp and if the country is diffrent it also show the same time stamp so how to i do tit in right way – Burhan Khanzada Feb 06 '17 at 13:22
  • In Database, store your time stamp in UTC and then create one method which converts UTC to your device local time. And use time after conversion from UTC to local. UTC to local : http://stackoverflow.com/a/17678000/1567907 – Rikin Prajapati Feb 06 '17 at 13:27
1

Use one of DateUtils's getRelative* functions.

Sangharsh
  • 2,999
  • 2
  • 15
  • 27
0

Updating your timestamp every second is unnecessary. I would suggest you to do it every minute. For your chat app I assume you have a time of your message received. In your activity or fragment, create a broadcast receiver and use the default ACTION_TIME_TICK Intent to receive updates every minute like this. I suggest you to receive the time as UNIX stamp.

BroadcastReceiver mBroadcastReceiver;
long messageTime = message.getTime() // get your time from the message.
TextView textView;

@Override
public void onStart() {
    super.onStart();
    mBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().compareTo(Intent.ACTION_TIME_TICK) == 0)
                    textView.setText((System.currentTimeMillis-messageTime)/1000L);
        }
    };



 registerReceiver(_broadcastReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));
}


@Override
public void onStop() {
super.onStop();
if(mBroadcastReceiver != null) 
unregisterReceiver(mBroadcastReceiver);

Edit: I'm in a hurry. I think you can come up with your logic to convert the seconds difference you get to context based text. This code displays time as seconds after update.

Ashik Vetrivelu
  • 1,021
  • 1
  • 9
  • 24