简体   繁体   中英

How to make timestamp for chat app like google messenger

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());

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.

使用DateUtilsgetRelative *函数之一。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM