Created
April 24, 2019 07:08
-
-
Save baldraider/ddc166a0ecc5a1bab4941055b9ef4320 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 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