Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public static String caluculateTimeAgo(long timeStamp) {
long timeDiffernce;
long unixTime = System.currentTimeMillis() / 1000L; //get current time in seconds.
int j;
String[] periods = {"s", "m", "h", "d", "w", "m", "y", "d"};
// you may choose to write full time intervals like seconds, minutes, days and so on
double[] lengths = {60, 60, 24, 7, 4.35, 12, 10};
timeDiffernce = unixTime - timeStamp;
String tense = "ago";
for (j = 0; timeDiffernce >= lengths[j] && j < lengths.length - 1; j++) {
timeDiffernce /= lengths[j];
}
return timeDiffernce + periods[j] + " " + tense;
}
@Fshamri

This comment has been minimized.

Copy link

@Fshamri Fshamri commented Jan 2, 2016

Hi, can you test for this timestamp 1451776013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment