You are on page 1of 1

import android.app.

Application;
import android.content.Context;

import java.text.SimpleDateFormat;
import java.util.Date;

public class RelativeTime extends Application {

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 = System.currentTimeMillis();


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

// TODO: localize
final long diff = now - time;
if (diff < MINUTE_MILLIS) {
return "Hace un momento";
} else if (diff < 2 * MINUTE_MILLIS) {
return "Hace un minuto";
} else if (diff < 50 * MINUTE_MILLIS) {
return "Hace " + diff / MINUTE_MILLIS + " minutos";
} else if (diff < 90 * MINUTE_MILLIS) {
return "Hace una hora";
} else if (diff < 24 * HOUR_MILLIS) {
return "Hace " + diff / HOUR_MILLIS + " horas";
} else if (diff < 48 * HOUR_MILLIS) {
return "Ayer";
} else {
return "Hace " + diff / DAY_MILLIS + " dias";
}
}

public static String timeFormatAMPM(long timestamp) {

SimpleDateFormat formatter = new SimpleDateFormat("hh:mm a");


String dateString = formatter.format(new Date(timestamp));

return dateString;
}

You might also like