Last active
February 5, 2017 11:44
-
-
Save Lzyct/7fea3e6df60897872be7767ea466fcbe to your computer and use it in GitHub Desktop.
Create time ago in android
This file contains hidden or 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 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