hienlt0610
3/4/2020 - 8:18 AM

[Java] Get human time

fun getTimeAgo(calendar: Calendar?): String? {
        if (calendar == null)
            return null
        val now = Calendar.getInstance().timeInMillis
        val time = calendar.timeInMillis
        val diff = now - time
        return if (diff < TimeUnit.MINUTES.toMillis(1)) {
            "Vừa xong"
        } else if (diff < 60 * MINUTE_MILLIS) {
            (diff / MINUTE_MILLIS).toString() + " phút trước"
        } else if (diff < 24 * HOUR_MILLIS) {
            (diff / HOUR_MILLIS).toString() + " giờ trước"
        } else if (diff < 2 * 24 * HOUR_MILLIS) {
            "Hôm qua"
        } else if (diff < 7 * 24 * HOUR_MILLIS) {
            (diff / DAY_MILLIS).toString() + " ngày trước"
        } else {
            getTimeDisplay(calendar)
        }
    }

    fun getTimeStill(dateTimeStart: Calendar?, timeStart: String): String? {
        if (dateTimeStart == null)
            return null
        if (TextUtils.isEmpty(timeStart)) {
            setTimeDefault(dateTimeStart, true)
        }
        //        if (TextUtils.isEmpty(timeEnd)) {
        //            setTimeDefault(dateTimeEnd, false);
        //        }
        val current = Calendar.getInstance()
        // on going
        //        if (current.getTimeInMillis() >= dateTimeStart.getTimeInMillis() && current.getTimeInMillis() <= dateTimeEnd.getTimeInMillis()) {
        //            return "Đang diễn ra";
        //        } else
        if (current.timeInMillis < dateTimeStart.timeInMillis) {
            if (TextUtils.isEmpty(timeStart) && current.get(Calendar.YEAR) == dateTimeStart.get(Calendar.YEAR)) {
                return "Còn " + (dateTimeStart.get(Calendar.DAY_OF_YEAR) - current.get(Calendar.DAY_OF_YEAR)) + " ngày nữa"
            }
            val now = current.timeInMillis
            val time = dateTimeStart.timeInMillis
            val diff = time - now
            return if (diff < 60 * MINUTE_MILLIS) {
                "Còn " + diff / MINUTE_MILLIS + " phút nữa"
            } else if (diff < 24 * HOUR_MILLIS) {
                "Còn " + diff / HOUR_MILLIS + " giờ nữa"
            } else if (diff < 365 * 24 * HOUR_MILLIS) {
                "Còn " + diff / DAY_MILLIS + " ngày nữa"
            } else {
                getTimeDisplay(dateTimeStart)
            }
        } else {
            return "Đã diễn ra"
        }
    }
/**
 * source : https://medium.com/@shaktisinh/time-a-go-in-android-8bad8b171f87
 */

public class TimeAgo {

    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;

    static String CONSTANT_TIME_just_now = "Just now";

    static String CONSTANT_TIME_a_minute_ago = "A minute ago";

    static String CONSTANT_TIME_minutes_ago = "minute ago";

    static String CONSTANT_TIME_an_hour_ago = "An hour ago";

    static String CONSTANT_TIME_hours_ago = "Hours ago";

    static String CONSTANT_TIME_yesterday = "Yesterday";

    static String CONSTANT_TIME_days_ago = "days ago";

    public static String get(long time) {

        if (time < 1000000000000L) {
            time *= 1000;
        }

        long now = System.currentTimeMillis();
        if (time > now || time <= 0) {
            return null;
        }

        final long diff = now - time;
        if (diff < MINUTE_MILLIS) {
            return CONSTANT_TIME_just_now;
        } else if (diff < 2 * MINUTE_MILLIS) {
            return CONSTANT_TIME_a_minute_ago;
        } else if (diff < 50 * MINUTE_MILLIS) {
            return diff / MINUTE_MILLIS + " " + CONSTANT_TIME_minutes_ago;
        } else if (diff < 90 * MINUTE_MILLIS) {
            return CONSTANT_TIME_an_hour_ago;
        } else if (diff < 24 * HOUR_MILLIS) {
            return diff / HOUR_MILLIS + " " + CONSTANT_TIME_hours_ago;
        } else if (diff < 48 * HOUR_MILLIS) {
            return CONSTANT_TIME_yesterday;
        } else {
            if (diff / DAY_MILLIS >= 5) {
                SimpleDateFormat newFormat = new SimpleDateFormat("dd MMM yy");
                return newFormat.format(new Date(time));
            } else {
                return diff / DAY_MILLIS + " " + CONSTANT_TIME_days_ago;
            }
        }
    }

}