Guest User

Untitled

a guest
Jul 17th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. const val TIMESTAMP_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" // eg. 2018-04-01T04:20:25.434Z
  2.  
  3. /**
  4. * Time converting from any timestamp pattern to "5 minutes ago", "1 day ago", etc.
  5. */
  6. fun convertTime(timestamp: String): String {
  7. val mDataFormat: DateFormat = SimpleDateFormat(TIMESTAMP_PATTERN, Locale.ENGLISH)
  8. return try {
  9. val date = mDataFormat.parse(timestamp)
  10. val relativeDateStr = DateUtils.getRelativeTimeSpanString(date.time, Calendar.getInstance().timeInMillis, DateUtils.MINUTE_IN_MILLIS)
  11. relativeDateStr.toString()
  12. } catch (e: ParseException) {
  13. Log.e("ParseException", "Unparseable date " + e.message)
  14. timestamp
  15. }
  16. }
Add Comment
Please, Sign In to add comment