Skip to content

Instantly share code, notes, and snippets.

@pedromassango
Created October 6, 2018 00:38
Show Gist options
  • Save pedromassango/93128cc1b338d0df19fa5f89b72eeea9 to your computer and use it in GitHub Desktop.
Save pedromassango/93128cc1b338d0df19fa5f89b72eeea9 to your computer and use it in GitHub Desktop.
package com.pedromassango.programmers.extras;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Process;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import static com.pedromassango.programmers.extras.Constants.AcountStatus.INCOMPLETE;
/**
* Created by Pedro Massango on 21-11-2016 at 21:35.
*/
public class Util {
// TIME AGO
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 concat(Object... args) {
String joineed = "";
for (Object s : args) {
joineed = joineed + " " + s;
}
return joineed;
}
public static String getTimeAgo(long time) {
String MIN = " min";
String HR = "h";
String HA = "há";
if (time < 1000000000000L) {
// if timestamp given in seconds, convert to millis
time *= 1000;
}
long now = System.currentTimeMillis();
if (time <= 0) {
return null;
} else if (time > now) {
time = now;
}
final long diff = now - time;
if (diff < MINUTE_MILLIS) {
return "agora";
} else if (diff < 2 * MINUTE_MILLIS) {
return concat(HA, join("1 ", MIN));
} else if (diff < 50 * MINUTE_MILLIS) {
return concat(HA, join((diff / MINUTE_MILLIS), MIN));
} else if (diff < 90 * MINUTE_MILLIS) {
return concat(HA, join("1 ", HR));
} else if (diff < 24 * HOUR_MILLIS) {
return concat(HA, join((diff / HOUR_MILLIS), HR));
} else if (diff < 48 * HOUR_MILLIS) {
return concat(HA, "1 d");
} else if ((diff / DAY_MILLIS) > 30) {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(time);
int day = c.get(Calendar.DAY_OF_MONTH);
int month = c.get(Calendar.MONTH) + 1;
int year = c.get(Calendar.YEAR);
return String.format("%s/%s/%s", day, month, year);
} else {
return concat(HA, join((diff / DAY_MILLIS), "d"));
}
}
public static long getTime() {
return System.currentTimeMillis();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment