Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmsherazi/5985a093076a8c4e7c38 to your computer and use it in GitHub Desktop.
Save dmsherazi/5985a093076a8c4e7c38 to your computer and use it in GitHub Desktop.
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
Copy link

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