Last active
May 2, 2023 22:35
-
-
Save dmsherazi/5985a093076a8c4e7c38 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
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
details are here http://dostmuhammad.com/blog/converting-unix-timestamp-to-seconds-minutes-hours-days-weeks-month-years-decades-ago/