Skip to content

Instantly share code, notes, and snippets.

@Lzyct
Last active February 5, 2017 11:44
Show Gist options
  • Select an option

  • Save Lzyct/7fea3e6df60897872be7767ea466fcbe to your computer and use it in GitHub Desktop.

Select an option

Save Lzyct/7fea3e6df60897872be7767ea466fcbe to your computer and use it in GitHub Desktop.
Create time ago in android
public class TimeAgoUtils{
public static String getValue(String yourPostingTime){
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", new Locale("id", "ID")); //set your locale
Date postDate;
Date currentDate;
try {
AppController.inputDate2.setTimeZone(TimeZone.getTimeZone("GMT+7")); //set your GMT server current time
postDate = sdf1.parse(yourPostingTime);
long postDateTime = postDate.getTime();
currentDate = new Date();
currentDate = sdf1.parse(sdf1.format(currentDate));
long currentDateTime = currentDate.getTime();
// Converting timestamp into x ago format
CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
Long.parseLong(String.valueOf(postDateTime)),
Long.parseLong(String.valueOf(currentDateTime)), DateUtils.SECOND_IN_MILLIS);
return timeAgo;
} catch (ParseException e) {
return e.toString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment