Skip to content

Instantly share code, notes, and snippets.

@baldraider
Created April 24, 2019 07:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baldraider/ddc166a0ecc5a1bab4941055b9ef4320 to your computer and use it in GitHub Desktop.
Save baldraider/ddc166a0ecc5a1bab4941055b9ef4320 to your computer and use it in GitHub Desktop.
public static String getDate(String time) {
DateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);
utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
Date date = utcFormat.parse(time);
long timeDiff = System.currentTimeMillis() - date.getTime();
if (timeDiff < 86400000) {
if (timeDiff < 60) {
return "Just now";
} else if (timeDiff < 120) {
return "1 minute ago";
} else if (timeDiff < 3600) {
return (int) timeDiff / 60 + " minutes ago";
} else if (timeDiff < 2 * 3600) {
return "1 hour ago";
} else {
return (int) timeDiff / 3600 + " hours ago";
}
} else {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(date.getTime());
return calendar.get(Calendar.DATE) + " " + mMonths[calendar.get(Calendar.MONTH)] + ", " + calendar.get(Calendar.YEAR);
}
} catch (ParseException e) {
e.printStackTrace();
}
return "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment